spring整合redis的配置檔案
阿新 • • 發佈:2018-12-21
說明:
1、檔案頭部分完整的除了redis還包含其他例如dubbo,aop等
2、 jedisPool,redisTemplate共用jedisPoolConfig,兩種可以同時存在,即在需要的類中@Autowired JedisPool,或者
RedisTemplate都行(至於說為什麼要這樣配,自己的Demo中比較一下兩種方式還是可以的)
3、redisHost port對應的conf.properties本文不貼
先放maven座標
<!-- jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.0</version> <type>jar</type> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis --> <!-- redisTemplate 是sprig-data下的 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.5.0.RELEASE</version> </dependency>
上圖pom檔案中spring-data-redis1.5.0跟 redis.clients2.8.0搭配好使,但是spring-data-redis1.8.6跟 redis.clients2.6.0就不行測試的時候jedisFactory建立不了,所以也需要注意一下。
標頭檔案:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> </beans>
1、單例項的jedis
<bean id="jedis" class="redis.clients.jedis.Jedis">
<constructor-arg name="host" value="${redis.host}" />
<constructor-arg name="port" value="${redis.port}" />
</bean>
@Autowired Jedis jedis;
2、JedisPool
<!--jedis配置--> <bean class="redis.clients.jedis.JedisPoolConfig" id="jedisPoolConfig"> <property name="maxIdle" value="50" /> <property name="maxTotal" value="500" /> <property name="maxWaitMillis" value="10000" /> <property name="testOnBorrow" value="true" /> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <property name="numTestsPerEvictionRun" value="100" /> </bean> <!-- JedisPool --> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg name="host" value="${redis.host}" /> <constructor-arg name="port" value="${redis.port}" /> <constructor-arg name="poolConfig" ref="jedisPoolConfig" /> </bean>
@Autowired JedisPool jedisPool;
3、redisTemplate
<bean class="redis.clients.jedis.JedisPoolConfig" id="jedisPoolConfig">
<property name="maxIdle" value="50" />
<property name="maxTotal" value="500" />
<property name="maxWaitMillis" value="10000" />
<property name="testOnBorrow" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<property name="numTestsPerEvictionRun" value="100" />
</bean>
<!-- 下面是配置模板方式 springData管理 redisTemplate -->
<!--jedisFactory-->
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
id="jedisConnFactory">
<property name="poolConfig" ref="jedisPoolConfig"/>
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="timeout" value="10000"/>
</bean>
<bean class="org.springframework.data.redis.core.RedisTemplate" id="redisTemplate">
<property name="connectionFactory" ref="jedisConnFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
@Autowired RedisTemplate redisTemplate;