spring java 方式配置JedisPool Bean
来自一个开源项目https://git.oschina.net/geek_qi/ace-cache
package com.ace.cache.config; import com.ace.cache.utils.PropertiesLoaderUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; import javax.annotation.PostConstruct; @Configuration
public class RedisConfig {
@Autowired
private Environment env;
private JedisPool pool;
private String maxActive;
private String maxIdle;
private String maxWait;
private String host;
private String password;
private String timeout;
private String database;
private String port;
private String enable;
private String sysName; @PostConstruct
public void init(){
PropertiesLoaderUtils prop = new PropertiesLoaderUtils("application.properties");
host = prop.getProperty("redis.host");
if(StringUtils.isBlank(host)){
host = env.getProperty("redis.host");
maxActive = env.getProperty("redis.pool.maxActive");
maxIdle = env.getProperty("redis.pool.maxIdle");
maxWait = env.getProperty("redis.pool.maxWait");
password = env.getProperty("redis.password");
timeout = env.getProperty("redis.timeout");
database = env.getProperty("redis.database");
port = env.getProperty("redis.port");
sysName = env.getProperty("redis.sysName");
enable = env.getProperty("redis.enable");
} else{
maxActive = prop.getProperty("redis.pool.maxActive");
maxIdle = prop.getProperty("redis.pool.maxIdle");
maxWait = prop.getProperty("redis.pool.maxWait");
password = prop.getProperty("redis.password");
timeout = prop.getProperty("redis.timeout");
database = prop.getProperty("redis.database");
port = prop.getProperty("redis.port");
sysName = prop.getProperty("redis.sysName");
enable = prop.getProperty("redis.enable");
}
} @Bean
public JedisPoolConfig constructJedisPoolConfig() {
JedisPoolConfig config = new JedisPoolConfig();
// 控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
// 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
config.setMaxTotal(Integer.parseInt(maxActive));
// 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
config.setMaxIdle(Integer.parseInt(maxIdle));
// 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
config.setMaxWaitMillis(Integer.parseInt(maxWait));
config.setTestOnBorrow(true); return config;
} @Bean(name = "pool")
public JedisPool constructJedisPool() {
String ip = this.host;
int port = Integer.parseInt(this.port);
String password = this.password;
int timeout = Integer.parseInt(this.timeout);
int database = Integer.parseInt(this.database);
if (null == pool) {
if (StringUtils.isBlank(password)) {
pool = new JedisPool(constructJedisPoolConfig(), ip, port, timeout);
} else {
pool = new JedisPool(constructJedisPoolConfig(), ip, port, timeout, password, database);
}
}
return pool;
} public String getMaxActive() {
return maxActive;
} public void setMaxActive(String maxActive) {
this.maxActive = maxActive;
} public String getMaxIdle() {
return maxIdle;
} public void setMaxIdle(String maxIdle) {
this.maxIdle = maxIdle;
} public String getMaxWait() {
return maxWait;
} public void setMaxWait(String maxWait) {
this.maxWait = maxWait;
} public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getTimeout() {
return timeout;
} public void setTimeout(String timeout) {
this.timeout = timeout;
} public String getDatabase() {
return database;
} public void setDatabase(String database) {
this.database = database;
} public String getSysName() {
return sysName;
} public void setSysName(String sysName) {
this.sysName = sysName;
} public String getEnable() {
return enable;
} public void setEnable(String enable) {
this.enable = enable;
} public String getPort() {
return port;
} public void setPort(String port) {
this.port = port;
}
}
spring java 方式配置JedisPool Bean的更多相关文章
- Java方式配置Spring MVC
概述 使用Java方式配置Spring MVC,以及回顾一下Spring MVC的各种用法. Spring MVC简述 关于Spring MVC的介绍网上有很多,这里就不再赘述了,只是要说一下,Spr ...
- Java方式配置Spring
概述 本文主要讲的是如何使用Java Bean来配置Spring,而不是用xml来配置Spring. 本文主要是代码,需要注意的都在注释里面. 代码打包下载地址(注:项目使用Maven构建) Java ...
- spring 注解方式配置Bean
Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件特定组件包括: @Component:基本注解,标示了一个受Spring管理的Bean组件 @Respository:标识 ...
- spring aop方式配置事务中的三个概念 pointcut advice advisor
AOP的3个关键概念 因为AOP的概念难于理解,所以在前面首先对Java动态代理机制进行了一下讲解,从而使读者能够循序渐进地来理解AOP的思想. 学习AOP,关键在于理解AOP的思想,能够使用AOP. ...
- Spring 通过XML配置装配Bean
使用XML装配Bean需要定义对于的XML,需要引入对应的XML模式(XSD)文件,这些文件会定义配置Spring Bean的一些元素,简单的配置如下: <?xml version=" ...
- spring注解方式配置以及spring4的泛型注入 (4)
目录 一.@Controller 注解控制层(action) 二.@Service 注解服务层 三.@Repository 持久层 四.spring4的泛型注入测试 1 创建两个实体User和Role ...
- Spring注解方式配置Redis
@Configuration public class RedisConfiguraion { @Bean public JedisConnectionFactory redisConnectionF ...
- spring java config配置搭建工程资料收集(网文)
https://blog.csdn.net/poorcoder_/article/details/70231779 https://github.com/lovelyCoder/springsecur ...
- Spring入门学习笔记(2)——基于Java的配置
目录 基于Java的配置 @Configuration & @Bean Annotations Example 注入Bean依赖 @Import注解 Lifecycle Callbacks(声 ...
随机推荐
- LR脚本用户自定义C语言函数
LR脚本实战:用户自定义C语言函数 Loadrunner可以使用标准C语言的函数,因此我们可以在脚本中编写自己的函数用于调用,把脚本结构化,更好的进行重用. 先看一个例子: Action() { in ...
- CentOS 7.4 上如何安装 tomcat 9
本文将详细讲解在 CentOS 7.4 系统上如何安装tomcat 9,tomcat是没有32位和64位之分的. 创建tomcat的安装路径 首先在/usr/local/下建立一个tomcat的文件夹 ...
- 【剑指offer】面试题 16. 数值的整数次方
面试题 16. 数值的整数次方 题目描述 题目:给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 解答过程 下面的讨论中 x 代表 bas ...
- Single Number III(LintCode)
Single Number III Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example G ...
- Spiral Matrix(LintCode)
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- changing chmod for files but not directories
find . -type f -print0 | xargs -0 chmod 644
- Python使用boto3操作AWS S3中踩过的坑
最近在AWS上开发部署应用. 看了这篇关于AWS中国区填坑的文章,结合自己使用AWS的经历,补充两个我自己填的坑. http://www.jianshu.com/p/0d0fd39a40c9?utm_ ...
- luogu P3375 【模板】KMP字符串匹配
题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果你不知道这是什么意思也不要问,去百度搜[ ...
- [CF600E]Dsu on tree
题意:树上每个点都有颜色,称一个颜色占领一棵子树,当且仅当没有别的颜色在这棵子树内的数量比它多.求所有子树的占领颜色之和.题解:最显然的是DFS序+主席树或莫队,这里使用Dsu on tree. 每次 ...
- [BZOJ 2298] Problem A
Link: BZOJ 2298 传送门 Solution: 可以将每个人的话转化为$[l[i],r[i]]$的人得分相同 用$map$记录认为$[i,j]$相同的人数,$pos[i][j]$记录以$i ...