Spring基于注解讀取外部配置文件
一、使用注解@PropertySource
指定路徑
使用 @PropertySource 指定配置文件路徑,支持 properties 和 XML 的配置文件,但不支持 yml。
屬性賦值
可以用注解 @Value 對(duì)屬性直接賦值、${}獲取配置文件的值、SPEL表達(dá)式#{}。
直接賦值:@Value('name jack') 讀取配置文件:@Value('${user.age}') 指定默認(rèn)值:@Value('${user.desc:default desc}') 表示如果沒有user.desc的配置,則賦值為default desc SPEL表達(dá)式:@Value('#{’${user.username}’?.toUpperCase()}') 表示將從配置文件讀取的值轉(zhuǎn)為大寫,?可以不填,表示如果沒有user.username的配置,則忽略例子
config.properties內(nèi)容
ps.datasource.driverClassName=com.mysql.jdbc.Driverps.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=trueps.datasource.username=rootps.datasource.password=rootps.datasource.minIdle=1ps.datasource.maxPoolSize=10ps.datasource.connectionTimeout=3000ps.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 */@Componentpublic 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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. idea設(shè)置提示不區(qū)分大小寫的方法2. .NET SkiaSharp 生成二維碼驗(yàn)證碼及指定區(qū)域截取方法實(shí)現(xiàn)3. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說明(學(xué)習(xí))4. css代碼優(yōu)化的12個(gè)技巧5. CentOS郵件服務(wù)器搭建系列—— POP / IMAP 服務(wù)器的構(gòu)建( Dovecot )6. ASP.NET MVC通過勾選checkbox更改select的內(nèi)容7. 原生JS實(shí)現(xiàn)記憶翻牌游戲8. Django使用HTTP協(xié)議向服務(wù)器傳參方式小結(jié)9. django創(chuàng)建css文件夾的具體方法10. IntelliJ IDEA創(chuàng)建web項(xiàng)目的方法
