Spring基于注解讀取外部配置文件
一、使用注解@PropertySource
指定路徑
使用 @PropertySource 指定配置文件路徑,支持 properties 和 XML 的配置文件,但不支持 yml。
屬性賦值
可以用注解 @Value 對屬性直接賦值、${}獲取配置文件的值、SPEL表達式#{}。
- 直接賦值:@Value("name jack")
- 讀取配置文件:@Value("${user.age}")
- 指定默認值:@Value("${user.desc:default desc}") 表示如果沒有user.desc的配置,則賦值為default desc
- SPEL表達式:@Value("#{'${user.username}'?.toUpperCase()}") 表示將從配置文件讀取的值轉(zhuǎn)為大寫,?可以不填,表示如果沒有user.username的配置,則忽略
例子
config.properties內(nèi)容
ps.datasource.driverClassName=com.mysql.jdbc.Driver
ps.datasource.jdbcUrl=jdbc:mysql://localhost:3306/spring?useTimezone=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&tcpRcvBuf=1024000&useOldAliasMetadataBehavior=true&useSSL=false&rewriteBatchedStatements=true&useAffectedRows=true
ps.datasource.username=root
ps.datasource.password=root
ps.datasource.minIdle=1
ps.datasource.maxPoolSize=10
ps.datasource.connectionTimeout=3000
ps.datasource.idleTimeout=300000
配置類
/** * 使用@PropertySource指定具體的配置文件,用@Value設(shè)置具體的屬性值, 不支持yml */ @Component @PropertySource("classpath:config.properties") public class DbProperties { @Value("${ps.datasource.driverClassName}") private String driverClassName; @Value("${ps.datasource.jdbcUrl}") private String jdbcUrl; @Value("${ps.datasource.username}") private String username; @Value("${ps.datasource.password}") private String password; @Value("${ps.datasource.minIdle}") private int minIdle; @Value("${ps.datasource.maxPoolSize}") private int maxPoolSize; @Value("${ps.datasource.connectionTimeout}") private int connectionTimeout; @Value("${ps.datasource.idleTimeout}") private int idleTimeout; public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public String getJdbcUrl() { return jdbcUrl; } public void setJdbcUrl(String jdbcUrl) { this.jdbcUrl = jdbcUrl; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getMinIdle() { return minIdle; } public void setMinIdle(int minIdle) { this.minIdle = minIdle; } public int getMaxPoolSize() { return maxPoolSize; } public void setMaxPoolSize(int maxPoolSize) { this.maxPoolSize = maxPoolSize; } public int getConnectionTimeout() { return connectionTimeout; } public void setConnectionTimeout(int connectionTimeout) { this.connectionTimeout = connectionTimeout; } public int getIdleTimeout() { return idleTimeout; } public void setIdleTimeout(int idleTimeout) { this.idleTimeout = idleTimeout; } @Override public String toString() { return "DbProperties{" + "driverClassName='" + driverClassName + '\'' + ", jdbcUrl='" + jdbcUrl + '\'' + ", username='" + username + '\'' + ", password='" + password + '\'' + ", minIdle=" + minIdle + ", maxPoolSize=" + maxPoolSize + ", connectionTimeout=" + connectionTimeout + ", idleTimeout=" + idleTimeout + '}'; } }
二、使用Environment
/** * Environment可以獲取classpath下配置的屬性值,無需指定具體的配置文件。 不支持yml */ @Component public class UserProperties { @Autowired private Environment env; public String getUserName() { return env.getProperty("user.name"); } public String getPassword() { return env.getProperty("user.password"); } }
三、使用PropertiesLoaderUtils
try { Properties properties = PropertiesLoaderUtils.loadAllProperties("config.properties"); System.out.println(properties.getProperty("user.name")); } catch (IOException e) { e.printStackTrace(); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決Spring boot整合mybatis,xml資源文件放置及路徑配置問題
- 詳解Spring Boot 打包分離依賴JAR 和配置文件
- spring是如何解析xml配置文件中的占位符
- SpringBoot在yml配置文件中配置druid的操作
- 詳解SpringBoot配置文件啟動時動態(tài)配置參數(shù)方法
- 關(guān)于Springboot打成JAR包后讀取外部配置文件的問題
- SpringBoot 配置文件中配置的中文,程序讀取出來是亂碼的解決
- springboot的yml配置文件通過db2的方式整合mysql的教程
- 淺談SpringBoot2.4 配置文件加載機制大變化
- spring配置文件解析失敗報”cvc-elt.1: 找不到元素 ''''beans'''' 的聲明”異常解決
相關(guān)文章
java父子節(jié)點parentid樹形結(jié)構(gòu)數(shù)據(jù)的規(guī)整
這篇文章主要介紹了java父子節(jié)點parentid樹形結(jié)構(gòu)數(shù)據(jù)的規(guī)整,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Java?Stream?流中?Collectors.toMap?的用法詳解
這篇文章主要介紹了Stream?流中?Collectors.toMap?的用法,Collectors.toMap()方法是把List轉(zhuǎn)Map的操作,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-01-01Spring實戰(zhàn)之使用@Resource配置依賴操作示例
這篇文章主要介紹了Spring實戰(zhàn)之使用@Resource配置依賴操作,結(jié)合實例形式分析了Spring使用@Resource配置依賴具體步驟、實現(xiàn)及測試案例,需要的朋友可以參考下2019-12-12Java中Double、Float類型的NaN和Infinity的具體使用
Java在處理浮點數(shù)運算時,提供了NaN和Infinity兩個常量,本文主要介紹了Java中Double、Float類型的NaN和Infinity的具體使用,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06