Spring Boot 自动装配之 ConfigurationProperties

小咪咪 2021-12-15 07:33 610阅读 0赞

Spring Boot 自动装配之 ConfigurationProperties

Spring Boot特性

  1. Spring Boot介绍:
  2. Create stand-alone Spring applications
  3. -- 创建一个独立的Spring应用
  4. Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  5. -- 嵌入式的web服务器TomcatJettyUndertow(无需部署 war 文件)
  6. Provide opinionated 'starter' dependencies to simplify your build configuration
  7. -- 提供建议的 "启动" 依赖关系, 以简化生成配置
  8. Automatically configure Spring and 3rd party libraries whenever possible
  9. -- 尽可能自动配置 spring 和第三方库
  10. Provide production-ready features such as metrics, health checks and externalized configuration
  11. -- 具备为生产准备的特性,如指标、运行状况检查和外部化配置
  12. Absolutely no code generation and no requirement for XML configuration
  13. -- 尽可能的无需代码生成并且无XML配置

自动装配的过程

  1. 通过各种注解实现了类与类之间的依赖关系,容器在启动的时候Application.run,会调用EnableAutoConfigurationImportSelector.class的selectImports方法(其实是其父类的方法)
  2. selectImports方法最终会调用SpringFactoriesLoader.loadFactoryNames方法来获取一个全面的常用BeanConfiguration列表
  3. loadFactoryNames方法会读取FACTORIES_RESOURCE_LOCATION(也就是spring-boot-autoconfigure.jar 下面的spring.factories),获取到所有的Spring相关的Bean的全限定名ClassName,大概120多个
  4. selectImports方法继续调用filter(configurations, autoConfigurationMetadata);这个时候会根据这些BeanConfiguration里面的条件,来一一筛选,最关键的是
    @ConditionalOnClass,这个条件注解会去classpath下查找,jar包里面是否有这个条件依赖类,所以必须有了相应的jar包,才有这些依赖类,才会生成IOC环境需要的一些默认配置Bean
  5. 最后把符合条件的BeanConfiguration注入默认的EnableConfigurationPropertie类里面的属性值,并且注入到IOC环境当中

ConfigurationProperties属性自动装配

文件类型

配置文件支持yml和properties两种,application.properties文件和application.yml,这两个文件都可以被SpringBoot自动识别并加载,两种文件格式一般被放在src/main/resource目录下面。

除此之外就是自定义配置文件了,一般的格式都是.properties。

属性自动装配一

设置属性配置application.properties

  1. test.config.userName=sxs
  2. test.config.password=123456
  3. test.config.email=shang_xxxxx@163.com
  4. test.config.phoneNumber=176xxxx1573

属性映射类

  1. @Configuration
  2. @ConfigurationProperties(prefix = "test.config")
  3. // @PropertySource("classpath:/test-config.properties")
  4. public class TestConfiguration {
  5. private String userName;
  6. private String password;
  7. private String email;
  8. private String phoneNumber;
  9. public String getUserName() {
  10. return userName;
  11. }
  12. public void setUserName(String userName) {
  13. this.userName = userName;
  14. }
  15. public String getPassword() {
  16. return password;
  17. }
  18. public void setPassword(String password) {
  19. this.password = password;
  20. }
  21. public String getEmail() {
  22. return email;
  23. }
  24. public void setEmail(String email) {
  25. this.email = email;
  26. }
  27. public String getPhoneNumber() {
  28. return phoneNumber;
  29. }
  30. public void setPhoneNumber(String phoneNumber) {
  31. this.phoneNumber = phoneNumber;
  32. }
  33. @Override
  34. public String toString() {
  35. return "测试配置结果{" +
  36. "userName='" + userName + '\'' +
  37. ", password='" + password + '\'' +
  38. ", email='" + email + '\'' +
  39. ", phoneNumber='" + phoneNumber + '\'' +
  40. '}';
  41. }
  42. }

测试类

  1. @RestController
  2. public class HelloWorldRestController {
  3. @Autowired
  4. private TestConfiguration configuration;
  5. @GetMapping(value = "/test-config")
  6. public String testConfiguration(@RequestParam(required = false) String message) {
  7. return configuration.toString();
  8. }
  9. }

测试结果
在这里插入图片描述

属性自动装配一

设置属性配置test.properties

  1. test.config.userName=sxs
  2. test.config.password=123456
  3. test.config.email=shang_xxxxx@163.com
  4. test.config.phoneNumber=176xxxx1573

其余代码不变,测试执行结果
在这里插入图片描述
修改属性配置类

  1. @Configuration
  2. @ConfigurationProperties(prefix = "test.config")
  3. @PropertySource("classpath:/test-config.properties")
  4. public class TestConfiguration {

测试结果
在这里插入图片描述

结论

在org.springframework.boot.context.config.ConfigFileApplicationListener中会自动加载application.properties文件和application.yml文件,具体描述信息如下:

  1. {@link EnvironmentPostProcessor} that configures the context environment by loading
  2. properties from well known file locations. By default properties will be loaded from
  3. 'application.properties' and/or 'application.yml' files in the following locations:
  4. 加载示例:
  5. <ul>
  6. <li>classpath:</li>
  7. <li>file:./</li>
  8. <li>classpath:config/</li>
  9. <li>file:./config/:</li>
  10. </ul>

使用@Configuration标注,加上@PropertySource(“classpath:test.properties”)注解,类的内部并不需要任何内容,这是一个纯粹的配置加载类。由于@Configuration的作用(底层为@Component),他会被Spring的扫描器扫到,并加载到JVM,并创建Bean,而创建的时候就会执行配置文件中配置项的加载。

更多优秀文章

代码已经在GitHub中更新,更多详情可关注dwyanewede。

JDK动态代理实现原理
https://blog.csdn.net/shang_xs/article/details/92772437
java界的小学生
https://blog.csdn.net/shang_xs

公众号推荐

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,610人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Boot 自动装配原理

    > 本文已经收录到Github仓库,该仓库包含计算机基础、Java核心知识点、多线程、JVM、常见框架、分布式、微服务、设计模式、架构等核心知识点,欢迎star~ > > G