@ConfigurationProperties加载外部配置方式
作者:oneslide 时间:2023-11-09 01:14:32
@ConfigurationProperties加载外部配置
@ConfigurationProperties
可以将外部配置文件(比如applicaition.properties
)加载进来,填充对象的对应字段的数据,然后供其他Bean使用。
整个项目结构是这样的:
这里我写了一个外部配置文件(application.yml):
environment:
production:
url: http://production.example.com
name: production mode
dev:
url: http://dev.example.com
name: developer mode
可以看见配置文件的数据有结构化特点,即可以生成一个相应的对象。
应该是这个样子:
class Environment{
Production production;
Dev dev;
class Production{
String url;
String name;
}
class Dev{
String url;
String name;
}
}
真实的代码是这个样子:
@Configuration //配置类注解,被自动扫描发现
@PropertySource("classpath:application.yml") //指明配置源文件位置
@ConfigurationProperties("environment") //指明前缀
public class EnvConfig {
private final Dev dev=new Dev();
private final Production production=new Production();
public Dev getDev() {
return dev;
}
//getXXX必须中xxx和字段名production严格一致,否则出异常,即使松弛绑定,也要这样
public Production getProduction() {
return production;
}
public static class Dev{
private String url;
private String name;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//toString
}
public static class Production{
private String url;
private String name;
//setter and getter省略
//toString
}
//toString()
}
下面测试一下:
@Controller
public class TestStaticController {
Logger log= LoggerFactory.getLogger(TestStaticController.class);
@Autowired
public EnvConfig env;
@GetMapping("/envtest")
public String getEnv(){
log.info(this.env.toString());
return "test";
}
}
通过发送HTTP请求,将会在日志中看到:
可以看见,外部配置文件填充了对象EnvConfig
,并可以被容器自动注入到其他对象中。
另外,外部配置文件映射成对象还有其他技术如:映射成数组,Map…
@ConfigurationProperties与@EnableConfigurationProperties
@EnableConfigurationProperties注解的作用是:使用 @ConfigurationProperties 注解的类生效。
如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component,那么在IOC容器中是获取不到properties 配置文件转化的bean。说白了 @EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入
@EnableConfigurationProperties 文档中解释:
当@EnableConfigurationProperties注解应用到你的@Configuration时, 任何被@ConfigurationProperties注解的beans将自动被Environment属性配置。 这种风格的配置特别适合与SpringApplication的外部YAML配置进行配合使用。
@ConfigurationProperties(prefix = "service.properties")
public class HelloServiceProperties {
private static final String SERVICE_NAME = "test-service";
private String msg = SERVICE_NAME;
set/get
}
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)
public class HelloServiceAutoConfiguration {
}
@RestController
public class ConfigurationPropertiesController {
@Autowired
private HelloServiceProperties helloServiceProperties;
@RequestMapping("/getObjectProperties")
public Object getObjectProperties () {
System.out.println(helloServiceProperties.getMsg());
return myConfigTest.getProperties();
}
}
自动配置设置(只是在配置文件的)
service.properties.name=my-test-name
service.properties.ip=192.168.1.1
service.user=kayle
service.port=8080
一切正常,但是 HelloServiceAutoConfiguration 头部不使用 @EnableConfigurationProperties,测访问报错。
不使用 @EnableConfigurationProperties 进行注册,使用 @Component 注册
@ConfigurationProperties(prefix = "service.properties")
@Component
public class HelloServiceProperties {
private static final String SERVICE_NAME = "test-service";
private String msg = SERVICE_NAME;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
Controller 不变,一切正常,如果注释掉 @Component 测启动报错。
由此证明,两种方式都是将被 @ConfigurationProperties 修饰的类,加载到 Spring Env 中
来源:https://oneslide.blog.csdn.net/article/details/85720888
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java详细讲解堆排序与时间复杂度的概念
![](https://img.aspxhome.com/file/2023/7/58597_0s.png)
Android Service生命周期详解
![](https://img.aspxhome.com/file/2023/0/130330_0s.png)
使用Files.walkFileTree遍历目录文件
JVM常用垃圾收集器详细解说
![](https://img.aspxhome.com/file/2023/0/83100_0s.png)
C#中的Lazy如何使用详解
Android Service启动流程刨析
![](https://img.aspxhome.com/file/2023/9/100649_0s.png)
C# 根据表格偶数、奇数加载不同颜色
![](https://img.aspxhome.com/file/2023/1/111071_0s.png)
Java如何实现上传文件到服务器指定目录
AJAX SpringBoot 前后端数据交互的项目实现
![](https://img.aspxhome.com/file/2023/7/59857_0s.png)
JavaWeb实现用户登录与注册功能(服务器)
![](https://img.aspxhome.com/file/2023/4/99714_0s.jpg)
SpringBoot整合Mybatis实现CRUD
![](https://img.aspxhome.com/file/2023/5/84765_0s.png)
Mybatis-Plus的多数据源你了解吗
![](https://img.aspxhome.com/file/2023/7/57617_0s.png)
springboot整合mybatis实现数据库的更新批处理方式
react native打包apk文件安装好之后进入应用闪退的解决方案
通过C#程序操作Config文件
java swing 创建一个简单的QQ界面教程
![](https://img.aspxhome.com/file/2023/4/67154_0s.jpg)
Android表格自定义控件使用详解
![](https://img.aspxhome.com/file/2023/7/138237_0s.jpg)
聊聊Spring AOP @Before @Around @After等advice的执行顺序
![](https://img.aspxhome.com/file/2023/3/86853_0s.jpg)