1.mongoDB配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> <!-- 配置 spring-data 连接 mongo 的mongoTemplate --> <!--首先列一下WriteConcern的几种抛出异常的级别参数: WriteConcern.NONE:没有异常抛出 WriteConcern.NORMAL:仅抛出网络错误异常,没有服务器错误异常 WriteConcern.SAFE:抛出网络错误异常、服务器错误异常;并等待服务器完成写操作。 WriteConcern.MAJORITY: 抛出网络错误异常、服务器错误异常;并等待一个主服务器完成写操作。 WriteConcern.FSYNC_SAFE: 抛出网络错误异常、服务器错误异常;写操作等待服务器将数据刷新到磁盘。 WriteConcern.JOURNAL_SAFE:抛出网络错误异常、服务器错误异常;写操作等待服务器提交到磁盘的日志文件。 WriteConcern.REPLICAS_SAFE:抛出网络错误异常、服务器错误异常;等待至少2台服务器完成写操作。 --> <mongo:mongo-client replica-set="${im.mongodb.replica.set}" > <mongo:client-options write-concern="NORMAL" /> </mongo:mongo-client> <!-- mongo模板类实例 --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo" /> <constructor-arg name="databaseName" value="${im.mongodb.name}" /> </bean> </beans>
2.redis连接配置--单机及集群
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <beans profile="dev,test"> <!--redis 配置--> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redisMaxTotal}" /><!-- 最大连接数 --> <property name="maxIdle" value="${redisMaxIdle}" /><!-- 最大空闲连接数 --> <property name="maxWaitMillis" value="${redisMaxWaitMillis}" /><!-- 获取连接时的最大等待毫秒数 --> <property name="testOnBorrow" value="${redisTestOnBorrow}" /><!-- 获取连接的时候检查有效性 --> </bean> <!-- redis的连接池实例--> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1" value="${redisHost}" /> <constructor-arg index="2" value="${redisPort}" type="int" /> <constructor-arg index="3" value="${redisTimeout}" type="int"/> <constructor-arg index="4" value="${redisPassword}"/> </bean> <!-- redis模板实例--> <bean id="jedisTemplate" class="cn.com.mx.flash.util.redis.SimpleJedisTemplate"> <constructor-arg name="jedisPool" ref="jedisPool"/> </bean> </beans> <beans profile="pre,prd"> <!--redis连接池配置 --> <bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" > <property name="maxTotal" value="${redisMaxTotal}" /><!-- 最大连接数 --> <property name="maxIdle" value="${redisMaxIdle}" /><!-- 最大空闲连接数 --> <property name="maxWaitMillis" value="${redisMaxWaitMillis}" /><!-- 获取连接时的最大等待毫秒数 --> <property name="testOnBorrow" value="${redisTestOnBorrow}" /><!-- 获取连接的时候检查有效性 --> </bean> <!-- redis集群实例 --> <bean id="jedisCluster" class="cn.com.mx.flash.util.redis.JedisClusterFactory"> <property name="redisServers" value="${redisServers}"/> <property name="redisTimeout" value="${redisTimeout}"/> <property name="maxRedirections" value="${redisMaxRedirections}"/> <property name="poolConfig" ref="genericObjectPoolConfig"/> </bean> <!-- redis模板实例--> <bean id="jedisTemplate" class="cn.com.mx.flash.util.redis.SimpleJedisTemplate"> <constructor-arg name="jedisCluster" ref="jedisCluster"/> </bean> </beans> </beans>
3.mysql连接配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 数据访问层配置 --> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:config/config.properties"/> <!-- 数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <!-- 配置初始化大小、最小、最大 --> <!-- 通常来说,只需要修改initialSize、minIdle、maxActive --> <property name="initialSize" value="1" /> <property name="minIdle" value="1" /> <property name="maxActive" value="20" /> <!--对于长时间不使用的连接强制关闭 --> <property name="removeAbandoned" value="true" /> <!--超过指定时间的空闲连接 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="3000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 解密密码必须要配置的项 --> <property name="filters" value="config" /> <property name="connectionProperties" value="config.decrypt=false" /> </bean> <!-- sqlsessionfactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property> <property name="dataSource" ref="dataSource"></property> </bean> <!-- mapper代理配置 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.lexiang.main.mapper.orm" /> <!-- 这里不能使用的配置 name=sqlSessionFactory 这样子会导致 数据源中spring注入项还未注入就初始化了数据源--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- 原始灵活的方式 配置 --> <!-- SqlSession模板类实例 --> <!-- <bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="close"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> --> </beans>