spring 配置文件配置redis、mysql、hibernate等

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_40053398/article/details/88667603

spring 配置文件配置redis、mysql、hibernate等

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
			xmlns="http://www.springframework.org/schema/beans" 
			xmlns:context="http://www.springframework.org/schema/context" 
			xmlns:aop="http://www.springframework.org/schema/aop" 
			xmlns:tx="http://www.springframework.org/schema/tx" 
			xsi:schemaLocation="http://www.springframework.org/schema/beans 
								http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
								http://www.springframework.org/schema/context 
								http://www.springframework.org/schema/context/spring-context-4.2.xsd 
								http://www.springframework.org/schema/aop 
								http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
								http://www.springframework.org/schema/tx 
								http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
	
	<!-- 指定spring读取db.properties配置 -->
	<context:property-placeholder location="classpath:*.properties"/>
	
	<!-- 开启扫描类中的注解 -->
	<context:component-scan base-package="com.hrcz"></context:component-scan>
	<!-- 配置c3p0连接池 -->
	<bean name="dataSources" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<!--连接池中保留的最大连接数。默认值: 15 -->   
        <property name="maxPoolSize" value="20"/>  
        <!-- 连接池中保留的最小连接数,默认为:3-->  
        <property name="minPoolSize" value="2"/>  
        <!-- 初始化连接池中的连接数,取值应在minPoolSize与maxPoolSize之间,默认为3-->  
        <property name="initialPoolSize" value="2"/>  
  
        <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0 -->   
        <property name="maxIdleTime" value="60"/>
            
        <!-- 当连接池连接耗尽时,客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒。默认: 0 -->   
        <property name="checkoutTimeout" value="3000"/>  
            
        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3 -->   
        <property name="acquireIncrement" value="2"/>  
  
        <!--定义在从数据库获取新连接失败后重复尝试的次数。默认值: 30 ;小于等于0表示无限次-->   
        <property name="acquireRetryAttempts" value="0"/>  
  
        <!--重新尝试的时间间隔,默认为:1000毫秒-->   
        <property name="acquireRetryDelay" value="1000" />  
  
        <!--关闭连接时,是否提交未提交的事务,默认为false,即关闭连接,回滚未提交的事务 -->   
        <property name="autoCommitOnClose" value="false"/>  
  
        <!--c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试使用。默认值: null -->   
        <property name="automaticTestTable" value="Test"/>  
  
        <!--如果为false,则获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常,但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。默认: false-->   
        <property name="breakAfterAcquireFailure" value="false"/>  
  
        <!--每60秒检查所有连接池中的空闲连接。默认值: 0,不检查 -->   
        <property name="idleConnectionTestPeriod" value="60"/>  
        <!--c3p0全局的PreparedStatements缓存的大小。如果maxStatements与maxStatementsPerConnection均为0,则缓存不生效,只要有一个不为0,则语句的缓存就能生效。如果默认值: 0-->   
        <property name="maxStatements" value="100"/> 
	</bean>
	
	<!-- 核心事务管理器 -->
	<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSources"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<!-- 可选配置 -->
				<prop key="hibernate.show_sql">false</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.autoReconnect">true</prop>
			</props>
		</property>
		<!-- 引入orm元数据 ,指定orm元数据所在包的路径,spring会自动读取包中的所有配置-->
		<property name="mappingDirectoryLocations" value="classpath:com/hrcz/domain"></property>
	</bean>
	
	<!-- jedisPool -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
	 	<!-- 最大连接数 --> 
		<property name="maxTotal" value="30" /> 
		<!-- 最大空闲连接数 --> 
		<property name="maxIdle" value="10" /> 
		<!-- 每次释放连接的最大数目 --> 
		<property name="numTestsPerEvictionRun" value="1024" /> 
		<!-- 释放连接的扫描间隔(毫秒) --> 
		<property name="timeBetweenEvictionRunsMillis" value="30000" /> 
		<!-- 连接最小空闲时间 --> 
		<property name="minEvictableIdleTimeMillis" value="1800000" /> 
		<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 --> 
		<property name="softMinEvictableIdleTimeMillis" value="10000" /> 
		<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 --> 
		<property name="maxWaitMillis" value="1500" /> 
		<!-- 在获取连接的时候检查有效性, 默认false --> 
		<property name="testOnBorrow" value="true" /> 
		<!-- 在空闲时检查有效性, 默认false --> 
		<property name="testWhileIdle" value="true" /> 
		<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true --> 
		<property name="blockWhenExhausted" value="false" /> 
	</bean>
	
	<!-- 配置单机版redis的连接 -->
	<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
		<constructor-arg name="poolConfig" ref="jedisPoolConfig" /> 
        <constructor-arg name="host" value="${redis.host}" /> 
        <constructor-arg name="port" value="${redis.port}" type="int" /> 
        <constructor-arg name="timeout" value="${redis.timeout}" type="int" /> 
        <constructor-arg name="password" value="#{'${redis.password}'!=''?'${redis.password}':null}" /> 
        <constructor-arg name="database" value="${redis.db.index}" type="int" /> 
	</bean>
	
	<bean id="jedisClientPool" class="com.hrcz.redis.JedisClientPool"/>
	
</beans>

猜你喜欢

转载自blog.csdn.net/qq_40053398/article/details/88667603
今日推荐