1. 程式人生 > >springboot 2.1.3.RELEASE版本解析.properties文件配置

springboot 2.1.3.RELEASE版本解析.properties文件配置

tweene cli 連接池 evict 文件內容 取出 false col 客戶

1、有時為了管理一些特定的配置文件,會考慮單獨放在一個配置文件中,如redis.properties:

#Matser的ip地址
redis.host=192.168.5.234
#端口號
redis.port=6379
#如果有密碼
redis.password=root
#客戶端超時時間單位是毫秒 默認是2000
redis.timeout=10000
#最大空閑數
redis.maxIdle=300
#連接池的最大數據庫連接數。設為0表示無限制,如果是jedis 2.4以後用redis.maxTotal
#redis.maxActive=600
#控制一個pool可分配多少個jedis實例,用來替換上面的redis.maxActive,如果是jedis 2.4以後用該屬性
redis.maxTotal=1000
#最大建立連接等待時間。如果超過此時間將接到異常。設為-1表示無限制。
redis.maxWaitMillis=1000
#連接的最小空閑時間 默認1800000毫秒(30分鐘)
redis.minEvictableIdleTimeMillis=300000
#每次釋放連接的最大數目,默認3
redis.numTestsPerEvictionRun=1024
#逐出掃描的時間間隔(毫秒) 如果為負數,則不運行逐出線程, 默認-1
redis.timeBetweenEvictionRunsMillis=30000
#是否在從池中取出連接前進行檢驗,如果檢驗失敗,則從池中去除連接並嘗試取出另一個
redis.testOnBorrow=true
#在空閑時檢查有效性, 默認false
redis.testWhileIdle=true

對於springboot框架來說,可以使用加入相關註解將配置文件解析成對應的對象,方便在代碼中使用

package com.example.springbootredis.config;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.Protocol; @Configuration //redis前綴 @ConfigurationProperties(prefix = "redis") //配置文件內容 @PropertySource("classpath:/config/redis.properties") @Data public class RedisConfig { private Logger logger = LoggerFactory.getLogger(RedisConfig.class); /** * Matser的ip地址 */ private String host; /** * 端口號 */ private Integer port; /** * 密碼 */ private String password; /** * 客戶端連接超時時間,單位毫秒,默認是2000 */ private Integer timeout; /** * 最大空閑數 */ private Integer maxIdle; /** * 控制一個pool可分配多少個jedis實例,如果是jedis 2.4以後用該屬性,2.4之前用的redis.maxActive */ private Long maxTotal; /** * 最大建立連接等待時間。如果超過此時間將接到異常。設為-1表示無限制。 */ private Long maxWaitMillis; /** * 連接的最小空閑時間 默認1800000毫秒(30分鐘) */ private Long minEvictableIdleTimeMillis; /** * 每次釋放連接的最大數目,默認3 */ private Long numTestsPerEvictionRun; /** * 逐出掃描的時間間隔(毫秒) 如果為負數,則不運行逐出線程, 默認-1 */ private Long timeBetweenEvictionRunsMillis; /** * 是否在從池中取出連接前進行檢驗,如果檢驗失敗,則從池中去除連接並嘗試取出另一個 */ private Boolean testOnBorrow; /** * 在空閑時檢查有效性, 默認false */ private boolean testWhileIdle; @Override public String toString() { return String.format("RedisConfig[host=%s,port=%s,password=%s,timeout=%s]", host, port, password, timeout); } }

這裏主要使用了@Configuration,@ConfigurationProperties(prefix = "redis"),@PropertySource("classpath:/config/redis.properties")三個註解,springboot以前的版本是使用

@Configuration,@ConfigurationProperties(prefix = "redis",locations="classpath:/config/redis.properties")

2、對於一些簡單的配置屬性可以放在application文件中,在代碼中使用@Value註解來獲取,如

@Value("${spring.redis.host}") String host

springboot 2.1.3.RELEASE版本解析.properties文件配置