redis + spring 集成

Posted #define wzz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis + spring 集成相关的知识,希望对你有一定的参考价值。

1、pom

<modelVersion>4.0.0</modelVersion>  
<groupId>com.x.redis</groupId>  
<artifactId>springredis</artifactId>  
<version>0.0.1-SNAPSHOT</version>  
  
<dependencies>  
    <dependency>  
        <groupId>org.springframework.data</groupId>  
        <artifactId>spring-data-redis</artifactId>  
        <version>1.0.2.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-test</artifactId>  
        <version>3.1.2.RELEASE</version>  
        <scope>test</scope>  
    </dependency>  
      
    <dependency>  
        <groupId>redis.clients</groupId>  
        <artifactId>jedis</artifactId>  
        <version>2.1.0</version>  
    </dependency>  
      
     <dependency>  
        <groupId>junit</groupId>  
        <artifactId>junit</artifactId>  
        <version>4.8.2</version>  
        <scope>test</scope>  
    </dependency>  
</dependencies>  

  2、spring配置文件(applicationContext.xml):

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<context:property-placeholder location="classpath:redis.properties" />

	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxIdle" value="${redis.maxIdle}" />
		<property name="maxActive" value="${redis.maxActive}" />
		<property name="maxWait" value="${redis.maxWait}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	</bean>
	
	<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
		p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"  p:pool-config-ref="poolConfig"/>
	
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" 	ref="connectionFactory" />
	</bean>		
	
	<bean id="userDao" class="com.x.dao.impl.UserDao" /> 
</beans>


//另一种
<bean id="jedisPool0" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
        <constructor-arg name="host" value="${redis.ip}" type="java.lang.String"/>
        <constructor-arg name="port" value="${redis.port}" type="int"/>
        <constructor-arg name="timeout" value="2000" type="int"/>
        <constructor-arg name="password" value="${redis.password}"/>
        <constructor-arg name="database" value="0" type="int"/>
    </bean>

  3、redis.properties

# Redis settings  
redis.host=localhost  
redis.port=6379  
redis.pass=java2000_wl  
  
  
redis.maxIdle=300  
redis.maxActive=600  
redis.maxWait=1000  
redis.testOnBorrow=true  

  

以上是关于redis + spring 集成的主要内容,如果未能解决你的问题,请参考以下文章

Spring + Jedis集成Redis

Spring Boot (24) 使用Spring Cache集成Redis

升级spring&集成Redis 二: spring4集成redis

Spring集成redis集群

spring boot 常见的第三方集成

Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)