Spring整合redis

pom.xml
<dependencies>
		<!-- Spring Data - Redis Library -->
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>1.4.0.RELEASE</version>
		</dependency>

		<!-- Jedis Driver Library -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.6.0</version>
		</dependency>

		<!-- Spring Core -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>

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

	</dependencies>


beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p"
	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
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- Scanning the Spring Beans -->
	<context:component-scan base-package="com.ljn.beans"></context:component-scan>

    <bean id="propertyConfigurerRedis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:redis.properties</value>
            </list>
        </property>
    </bean>


	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.pool.maxTotal}" />
		<property name="maxIdle" value="${redis.pool.maxIdle}" />
		<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
	</bean>

	<!-- Redis Connection Factory -->
	<bean id="jedisConnFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName" value="${redis.ip}" />
		<property name="port" value="${redis.port}" />
		<property name="poolConfig" ref="jedisPoolConfig" />
	</bean>

	<!-- Redis Template -->
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
		p:connection-factory-ref="jedisConnFactory" />

</beans>


redis.properties
redis.pool.maxTotal=1024
redis.pool.maxIdle=200
redis.pool.maxWaitMillis=3000
redis.pool.testOnBorrow=true

redis.ip=10.75.201.3
redis.port=63790


注意,旧版本的写法:

redis.pool. maxActive=1024

redis.pool.maxIdle=200

redis.pool. maxWait=1000

redis.pool.testOnBorrow=true

redis.ip=10.11.20.140

redis.port=6379

代码:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RegistrationBean {
	@Autowired
	private RedisTemplate<String,User> redisTemplate;

	public RedisTemplate<String, User> getRedisTemplate() {
		return redisTemplate;
	}

	public void setRedisTemplate(RedisTemplate<String, User> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}
}


  
		RegistrationBean bean = (RegistrationBean)context.getBean("registrationBean");

		// Persisting Inside the Hash User object
		bean.getRedisTemplate().opsForHash().put("{a}UserA", user.hashCode(),user);
		// Retrieving the User object from the Redis by using the suggested key
		User x = (User)bean.getRedisTemplate().opsForHash().get("{b}UserA", user.hashCode());
		System.out.println(x.getFullName());

注意到上面的getRedisTemplate().opsForHash().set()方法,第一个参数是key本身,第二个参数是object的hashcode,第三个参数是object本身。
例如可以写代码测试:

        user.setFullName("ljn");
        bean.getRedisTemplate().opsForHash().put("{b}UserA", user.hashCode(),user);
        // Retrieving the User object from the Redis by using the suggested key
        User y = (User)bean.getRedisTemplate().opsForHash().get("{b}UserA", user.hashCode());
        System.out.println(y.getFullName());

这个时候的key跟前面的一样,但取得的value不一样,为修改后的值,getFullName输出:ljn

你可能感兴趣的:(redis)