java 注解加载配置文件_Spring基于注解读取外部配置文件

本文详细介绍了在Spring中如何使用注解加载配置文件,包括@PropertySource、Environment和PropertiesLoaderUtils三种方式。示例代码展示了如何通过注解直接赋值、读取配置文件值及使用SPEL表达式,帮助开发者更好地理解和使用Spring的注解配置。
摘要由CSDN通过智能技术生成

一、使用注解@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()}") 表示将从配置文件读取的值转为大写,?可以不填,表示如果没有user.username的配置,则忽略

例子

config.properties内容

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设置具体的属性值, 不支持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();

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值