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的实例。
- spring-boot整合redis 总结
- spring boot整合redis
- spring boot 整合 redis
- spring boot整合redis
- Spring Boot整合Redis
- spring boot redis整合
- (七)Spring Boot 整合Redis
- spring boot shiro redis整合
- spring boot中spring cache 整合redis
- spring boot整合netty总结
- spring boot整合redis主从sentinel
- spring boot 中redis与cookie整合
- spring boot整合redis实现缓存机制
- Spring Boot 整合 Redis 实现缓存操作
- Spring Boot 整合 Redis 实现缓存操作
- Spring boot整合redis小例子
- Spring Boot 整合 Redis 实现缓存操作
- spring boot整合redis(单机+集群)
- JAVABEAN EJB POJO区别
- Sass的混合-@mixin,@include
- 透明位图测试
- fastdfs
- Linux Platform驱动模型(一) _设备信息
- spring-boot整合redis 总结
- ListView源码分析
- Linux 服务器添加双线 修改静态路由
- 快速查找得出索引位置
- Java String 编码格式转换
- centos 7 lamp+lnmp环境
- Handler.removeMessages的作用
- webpack 启动后自动打开浏览器插件:open-browser-webpack-plugin
- Uncaught RangeError: Invalid array length 问题解决