@ConfigurationProperties加载外部配置方式

作者:oneslide 时间:2023-11-09 01:14:32 

@ConfigurationProperties加载外部配置

@ConfigurationProperties可以将外部配置文件(比如applicaition.properties)加载进来,填充对象的对应字段的数据,然后供其他Bean使用。

整个项目结构是这样的:

@ConfigurationProperties加载外部配置方式

这里我写了一个外部配置文件(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;
    }
}

真实的代码是这个样子:

@ConfigurationProperties加载外部配置方式

@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请求,将会在日志中看到:

@ConfigurationProperties加载外部配置方式

可以看见,外部配置文件填充了对象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

标签:@ConfigurationProperties,加载,配置
0
投稿

猜你喜欢

  • Java详细讲解堆排序与时间复杂度的概念

    2023-10-20 02:00:11
  • Android Service生命周期详解

    2022-03-21 13:13:55
  • 使用Files.walkFileTree遍历目录文件

    2021-09-27 06:12:40
  • JVM常用垃圾收集器详细解说

    2023-02-15 02:19:49
  • C#中的Lazy如何使用详解

    2023-10-25 12:56:34
  • Android Service启动流程刨析

    2023-07-31 11:28:58
  • C# 根据表格偶数、奇数加载不同颜色

    2022-01-19 11:46:05
  • Java如何实现上传文件到服务器指定目录

    2021-10-16 14:38:31
  • AJAX SpringBoot 前后端数据交互的项目实现

    2023-11-24 05:49:48
  • JavaWeb实现用户登录与注册功能(服务器)

    2022-12-19 13:28:31
  • SpringBoot整合Mybatis实现CRUD

    2022-01-10 09:26:45
  • Mybatis-Plus的多数据源你了解吗

    2023-07-22 00:46:59
  • springboot整合mybatis实现数据库的更新批处理方式

    2023-11-29 07:08:37
  • react native打包apk文件安装好之后进入应用闪退的解决方案

    2022-11-04 06:13:09
  • 通过C#程序操作Config文件

    2023-11-30 14:24:06
  • java swing 创建一个简单的QQ界面教程

    2022-09-08 06:51:39
  • Android表格自定义控件使用详解

    2023-12-23 23:35:36
  • 聊聊Spring AOP @Before @Around @After等advice的执行顺序

    2022-10-27 18:14:20
  • mybatis <foreach>标签动态增删改查方式

    2022-07-27 20:03:16
  • C#泛型的逆变协变之个人理解

    2021-05-28 16:33:03
  • asp之家 软件编程 m.aspxhome.com