Springmvc整合redis

1.软件环境Springmvc4.1.1.RELEASE+redis服务端(2.8.19)

2.添加基于maven支持的redis.clients(最新的2.6.2)

     <!--redis-->
  <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.6.2</version>
  </dependency>

     <!-- spring-redis -->
  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.2.1.RELEASE</version>
  </dependency>

3.新建redis配置文件redis.properties

  #redis setting
  #ip
  redis.ip=127.0.0.1
  #port
  redis.port=6379
  #密码
  redis.pass=myredis123

  #最大能够保持idel状态的对象数
  redis.maxIdle=200
  #最大分配的对象数
  redis.maxTotal=512
  #当池内没有返回对象时,最大等待时间
  redis.maxWaitMillis=1000
  #当调用borrow Object方法时,是否进行有效性检查
  redis.testOnBorrow=true

4.在spring配置文件里面进行整合redis spring-redis.xml

  <!--redis资源文件-->
<context:property-placeholder location="classpath:redis.properties" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.ip}"/>
<property name="port" value="${redis.port}"/>
<property name="password" value="${redis.pass}"/>
<property name="poolConfig" value="poolConfig"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>

 

 

你可能感兴趣的:(springMVC)