本來想做一個類似數據庫連接池一樣的插件工具,可以在任意的項目工程中啓動的時候,直接加載配置的連接信息,而達到初始化連接池的目的;
方案一:使用java自帶的文件讀取工具類,配置讀取方法,先讀取配置文件,再初始化連接池(以redis連接池爲例)
//私有構造方法
private JedisPoolUtil(){}
//保證內存中只有一個連接池對象
public static JedisPool pool = null;
//靜態代碼塊
static{
//讀取資源文件
ResourceBundle bundle = ResourceBundle.getBundle("application");//application.properties只能讀取以.properties的配置文件
//讀取相應的值
JedisPoolConfig config = new JedisPoolConfig();
config.setMinIdle(Integer.parseInt(bundle.getString("minIdle")));//最小空閒連接數
config.setMaxIdle(Integer.parseInt(bundle.getString("maxIdle")));//最大空閒連接數
config.setMaxTotal(Integer.parseInt(bundle.getString("maxTotal")));//最大連接數
config.setMaxWaitMillis(Integer.parseInt(bundle.getString("maxWaitMillis")));//最大等待超時時間
logger.info("[{}] [{}] [{}] [{}] [{}] [{}]",bundle.getString("host"),Integer.parseInt(bundle.getString("port")),Integer.parseInt(bundle.getString("minIdle")),Integer.parseInt(bundle.getString("maxIdle")),Integer.parseInt(bundle.getString("maxTotal")),Integer.parseInt(bundle.getString("maxWaitMillis")));
pool = new JedisPool(config, bundle.getString("host"), Integer.parseInt(bundle.getString("port")));
}
//獲取連接
public static Jedis getJedis(){
return pool.getResource();
}
//關閉連接
public static void closeJedis(Jedis jedis) {
jedis.close();
}
application.properties
#REDIS CONNECTION INFORMATION
redis.host: 127.0.0.1
redis.port: 6379
redis.minIdle: 10
redis.maxIdle: 100
redis.maxTotal: 200
redis.maxWaitMillis:5000
方案二:使用spring自帶的文件讀取工具類,配置讀取方法,先讀取配置文件,再初始化連接池(以redis連接池爲例)
//私有構造方法
private JedisPoolUtil(){}
//保證內存中只有一個連接池對象
public static JedisPool pool = null;
//靜態代碼塊
static{
//讀取資源文件
//spring自帶的文件讀取工具類
Resource resource = new ClassPathResource("application.yml");
Properties bundle = PropertiesLoaderUtils.loadProperties(resource);
String host = bundle.getProperty("redis.host");
//讀取相應的值
JedisPoolConfig config = new JedisPoolConfig();
config.setMinIdle(Integer.parseInt(bundle.getProperty("redis.minIdle")));//最小空閒連接數
config.setMaxIdle(Integer.parseInt(bundle.getProperty("redis.maxIdle")));//最大空閒連接數
config.setMaxTotal(Integer.parseInt(bundle.getProperty("redis.maxTotal")));//最大連接數
config.setMaxWaitMillis(Integer.parseInt(bundle.getProperty("redis.maxWaitMillis")));//最大等待超時時間
logger.info("[{}] [{}] [{}] [{}] [{}] [{}]",bundle.getProperty("redis.host"),Integer.parseInt(bundle.getProperty("redis.port")),Integer.parseInt(bundle.getProperty("redis.minIdle")),Integer.parseInt(bundle.getProperty("redis.maxIdle")),Integer.parseInt(bundle.getProperty("redis.maxTotal")),Integer.parseInt(bundle.getProperty("redis.maxWaitMillis")));
pool = new JedisPool(config, host, Integer.parseInt(bundle.getProperty("redis.port")));
}
//獲取連接
public static Jedis getJedis(){
return pool.getResource();
}
//關閉連接
public static void closeJedis(Jedis jedis) {
jedis.close();
}
application.yml
#REDIS CONNECTION INFORMATION
redis.host: 127.0.0.1
redis.port: 6379
redis.minIdle: 10
redis.maxIdle: 100
redis.maxTotal: 200
redis.maxWaitMillis:5000
以上是初始化redis的連接池工具類(加上@Configuration註解),可以打成一個jar包,引入到想要使用的項目POM文件中,並在啓動類上加入需要加載的包路徑 @ComponentScan(basePackages = { "xxx.yyy.zzz"}),application.yml及application.properties配置文件是放在想引入項目的resources目錄下的,而不是工具類所在的目錄下的哦,
都配置好之後,就可以啓動項目,會發現我們想要初始化連接池的操作,也已經被項目給順便加入了spring管理了,並初始化完成。
方案三:既然我們都可以使用註解的方式成功加入到spring的管理中,爲什麼不直接使用註解來讀取配置文件,像項目本身的一個類一樣來配置和管理呢,不得不說,spring還真是強大啊。。。
application.yml
#REDIS CONNECTION INFOMATION
redis:
pool:
host: 127.0.0.1
port: 6379
@Component
@ConfigurationProperties(prefix = "redis")
public class RedisConnectConfig {
/**
* 從配置文件中讀取的limitSizeMap開頭的數據
* 注意:名稱必須與配置文件中保持一致
* 在代碼部分可直接使用pool.get("host")來得到“127.0.0.1”的數據
*/
private Map<String, String> pool= new HashMap<>();
public Map<String, String> getPool() {
return pool;
}
public void setPool(Map<String, String> pool) {
this.pool= pool;
}
}