美文网首页
Spring Boot读取配置的几种方式

Spring Boot读取配置的几种方式

作者: 一个骚骚的码农 | 来源:发表于2021-11-01 09:36 被阅读0次

在application.yml或者properties文件中添加:
info.address=USA
info.company=Spring
info.degree=high

@Value注解读取方式

@ConfigurationProperties注解读取方式

激活 @ConfigurationProperties
我们可以通过下面几种方式将其添加到应用上下文中
首先,我们可以通过添加 @Component 注解让 Component Scan 扫描到

读取指定文件

资源目录下建立config/db-config.properties:
db.username=root
db.password=123456

@PropertySource+@Value注解读取方式\

@PropertySource+@ConfigurationProperties注解读取方式

Environment读取方式

@Autowired
private Environment env;
// 获取参数
String getProperty(String key);

通过config设置开关

@conditionalonproperty(name = "XXX",havingValue = "true")

参考文献

https://www.cnblogs.com/jimoer/p/11374229.html

相关文章

网友评论

      本文标题:Spring Boot读取配置的几种方式

      本文链接:https://www.haomeiwen.com/subject/jxhzaltx.html