spring-boot整合redis 总结

来源:互联网 发布:c语言删除文件内容 编辑:程序博客网 时间:2024/06/06 00:51

spring-boot整合redis 总结
项目整合redis,如何实现最少写配置代码,从而实现redis客户端实例的注入。Spring提供了一些注解来帮助开发人员减少配置文件的编写。
1,创建RedisConfig类,使用@Configuration 和 @bean来管理 redis的工具类

@Configurationpublic class RedisConfig {@Autowiredprivate ShardedJedisPool shardedJedisPool;@Bean(name = "redisCacheManager")@Scope("singleton")public RedisCacheManager getRedisCacheManager() {        return new CacheManagerImpl(shardedJedisPool);    }}

使用@Configuration @bean 相当于是在applicationContext.xml文件中配置 一样。当Spring容器扫描到这个类的时候就会将RedisCacheManager的实例放到Ioc容器中。
我们后面就可以通过@Resource注解来调用这个类了。

2,RedisCacheManager只是一个接口,我们需要定义一个实现类,在实现类中来获取ShardedJedis的实例,ShardedJedis的实例是通过ShardedJedisPool对象来获取的。所以关键点
就是就是如何对ShardedJedisPool进行实例化,从而传递给实现类。

public interface RedisCacheManager {
public ShardedJedis getShardJedis();

}

public class CacheManagerImpl implements RedisCacheManager {

private ShardedJedisPool pool;public CacheManagerImpl() {}public CacheManagerImpl(ShardedJedisPool pool) {    this.pool = pool;}@Overridepublic ShardedJedis getShardJedis() {    ShardedJedis jedis = pool.getResource();    return jedis;}

}

3,最后的问题就是如何给ShardedJedisPool注入实例了,利用spring提供的FactoryBean接口,传入的泛型为ShardedJedisPool,最后在applicationContext.xml文件中配置
。最后实现了ShardedJedisPool实例化。

public class ShardedJedisPoolFactory implements         InitializingBean,DisposableBean,FactoryBean<ShardedJedisPool> {private JedisPoolConfig poolConfig = null;private int timeout;private String password;private String hostAndPorts;private int dbIndex;private ShardedJedisPool pool;@Overridepublic void afterPropertiesSet() throws Exception {}public ShardedJedisPoolFactory(){    this.poolConfig = new JedisPoolConfig();    this.hostAndPorts = "localhost:6379";    this.dbIndex = 0;    this.poolConfig.setMaxTotal(200);    this.poolConfig.setMaxIdle(10);}@Overridepublic ShardedJedisPool getObject() throws Exception {    if ("".equals(this.hostAndPorts)) return new ShardedJedisPool(new JedisPoolConfig(), new ArrayList<JedisShardInfo>());    List<JedisShardInfo> jedisShardInfos = new ArrayList<JedisShardInfo>();    for (String hp : this.hostAndPorts.split(",")) {        if ("".equals(hp)) continue;        String[] host_port_s = hp.split(":");        JedisShardInfo info = new JedisShardInfo(host_port_s[0], Integer.parseInt(host_port_s[1]));        jedisShardInfos.add(info);    }    pool = new ShardedJedisPool(this.poolConfig, jedisShardInfos);    return pool;}@Overridepublic Class<?> getObjectType() {    return ShardedJedisPool.class;}@Overridepublic boolean isSingleton() {    return true;}@Overridepublic void destroy() throws Exception {    try{        pool.destroy();    }catch (Exception e){    }}public JedisPoolConfig getPoolConfig() {    return poolConfig;}public void setPoolConfig(JedisPoolConfig poolConfig) {    this.poolConfig = poolConfig;}public int getTimeout() {    return timeout;}public void setTimeout(int timeout) {    this.timeout = timeout;}public String getPassword() {    return password;}public void setPassword(String password) {    this.password = password;}public String getHostAndPorts() {    return hostAndPorts;}public void setHostAndPorts(String hostAndPorts) {    this.hostAndPorts = hostAndPorts;}public int getDbIndex() {    return dbIndex;}public void setDbIndex(int dbIndex) {    this.dbIndex = dbIndex;}}<bean id="jedisPool" class="com.water.core.cache.ShardedJedisPoolFactory">    <property name="hostAndPorts" value="@redis.address@"/>    <!--<property name="hostAndPorts" value="127.0.0.1:6379"/>--></bean>

ShardedJedisPool实例化的工作也完成了,最后将该实例作为实现类的构造参数传递过去。在实现类中调用该实例获取ShardedJedis的实例。

0 0
原创粉丝点击