Spring集成Redis⽅案(spring-data-redis)(基于Jedis的单
机。。。
说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要⾃⾏引⼊,这个是个坑点。并且会与⼀些低版本的Spring有冲突,要看官⽅⽂档和不断的测试。
继上⼀篇⽂章中提到的⼏款客户端,它们基本都能和Spring集成。
下⾯介绍的是基于Spring原⽣的spring-data-redis去集成。
还要注意的是,Spring整合了Jedis框架进去,所以下⾯配置上还会基于Jedis去实现的,但是Spring在上层已经集成了很好⽤的⼯具类。
⽽整个框架基于Spring Data,⾥⾯集成了主流的应⽤,⽐如Redis,MongoDB等,⽽基于这些应⽤的⼀些⾮常好⽤的框架也集成了,⽐如Jedis这些。
我猜测,基于Spring Data Redis去集成的,底层还是⽤Jedis去实现,应该是可以实现客户端集的,下次我再实践详细分析⼀下。Spring Data Redis
springframework jar包下载
项⽬结构:
POM:
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-
instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId&dis</groupId>
<artifactId>Spring_redis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring_redis</name>
<url></url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<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-core</artifactId>
<version>3.1.2.RELEASE</version>
</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>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<!-- 将现有的jakarta commons logging的调⽤转换成lsf4j的调⽤。 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.1</version>
</dependency>
<!-- Hack:确保commons-logging的jar包不被引⼊,否则将和jcl-over-slf4j冲突 -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<!-- slf4j的实现:logback,⽤来取代log4j。更快、更强! -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>0.9.24</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
context:property-placeholder标签⽤来导⼊properties⽂件。从⽽替换${redis.maxIdle}这样的变量。context:component-scan是为了在dis.dao报下的类能够实⽤spring的注解注⼊的⽅式。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance" xmlns:p="/schema/p"
xmlns:context="/schema/context"
xmlns:jee="/schema/jee" xmlns:tx="/schema/tx"
xmlns:aop="/schema/aop"
xsi:schemaLocation="
/schema/beans /schema/beans/spring-beans.xsd
/schema/context /schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:redis.properties"/>
<context:component-scan base-package="dis.dao">
</context:component-scan>
<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="${stOnBorrow}"/>
</bean>
<bean id="connectionFactory" class="org.tion.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.StringRedisTemplate">
<property name="connectionFactory"  ref="connectionFactory"/>
</bean>
<bean id="userDAO" class="dis.dao.impl.UserDAOImpl"/>
</beans>
redis.properties:
# Redis settings
#redis.host=192.168.20.101
#redis.port=6380
#redis.pass=foobared
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
UserDAOImpl:
Spring对DAO层的封装很多⽤了类似于下⾯代码的模板⽅式。
RedisTemplate就是Spring对Redis的⼀个封装。
public class UserDAOImpl implements UserDAO {
@Autowired
protected RedisTemplate<Serializable, Serializable> redisTemplate;
public void saveUser(final User user) {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.StringSerializer().serialize("user.uid." + Id()),
return null;
}
});
}
@Override
public User getUser(final long id) {
ute(new RedisCallback<User>() {
@Override
public User doInRedis(RedisConnection connection) throws DataAccessException {
byte[] key = StringSerializer().serialize("user.uid." + id);
if (ists(key)) {
byte[] value = (key);
String name = StringSerializer().deserialize(value);
User user = new User();
user.setName(name);
user.setId(id);
return user;
}
return null;
}
});
}
}
User:
public class User {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试代码:
public static void main(String[] args) {
ApplicationContext ac =  new ClassPathXmlApplicationContext("classpath:/l");        UserDAO userDAO = (Bean("userDAO");
User user1 = new User();
user1.setId(1);
user1.setName("obama");
userDAO.saveUser(user1);
User user2 = User(1);        System.out.Name()); }
参考:
(以上内容转⾃此篇⽂章)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。