Spring Boot 配置文件

本文介绍了如何在Spring Boot项目中拆分配置文件,包括application-comm.yml, application-dev.yml, application-prod.yml和application-tv.yml,以及如何通过spring.profiles.group进行配置分组激活。同时讲解了使用@Value和@ConfigurationProperties注解来获取和绑定配置项,最后展示了测试配置项的方法和启动日志的观察方式。" 1456613,98747,绑定IList对象复合属性到DataGrid,"['Windows开发', 'WPF', '数据绑定', 'UI设计', 'datagrid']
摘要由CSDN通过智能技术生成

1. 项目结构

⏰ 项目结构
  • ⭕🌏 Spring Boot Demo 的项目结构如下:
    image
⏰ demo 目标
  • ⭕🌏 将单配置文件拆分为多配置文件,达到如下目的 ① 将 redis,mq,db 等配置文件拆分为不同的配置文件,然后在 application 中集成配置文件。② 指定不同的开发环境。

2. 项目配置文件

⏰ application-comm.yml
  • ⭕🌏 假设我们在配置中,抽取出通用配置如下:
    server:
      port: 8888
    application:
      name: Spring Boot Demo
      version: 2.4.1
    
⏰ application-dev.yml 和 application-prod.yml
  • ⭕🌏 假设配置文件 dev 为开发环境配置,prod 为生产环境配置:
    # dev
    teleplay:
      info:
        website: test.ohbee.com
        enabled: false
    
    # prod
    teleplay:
      info:
        website: ohbee.com
        enabled: true
    
⏰ application-tv.yml
  • ⭕🌏 另一份通用配置:
    teleplay:
      info:
        title: friends
        actors:
            - Joey
            - Phoebe
            - Rachel
            - Chandler
            - Ross
            - Monica
    
❗ application.yml
  • ⭕🌏 可以通过如下配置将分散的配置合并到一起,并激活:
    #-------------使用如下集成方式,可激活配置
    #spring:
    #  profiles:
    #    active: comm,dev,tv
    
    #-------------或者使用如下集成方式,可激活配置
    #spring:
    #     profiles:
    #       include: comm,dev,tv
    
    #-------------或者使用如下分组的集成方式,可激活配置
    spring:
     profiles:
       active: prod
       group:
         dev: comm,dev,tv
         prod: comm,prod,tv
    

    使用 spring.profiles.group 配置可以分组激活

3. 获取配置文件中的配置项

⏰ 使用 @Value 获取配置
  • ⭕🌏 @Value
    @Data
    @Component
    public class ApplicationProperty {
        @Value("${application.name}")
        private String name;
        @Value("${application.version}")
        private String version;
    }
    
⏰ 使用 @ConfigurationProperties 获取配置
  • ⭕🌏 @ConfigurationProperties
    @ConfigurationProperties("teleplay.info")
    @Component
    @Data
    public class TeleplayInfoProperty {
       private String website;
       private Boolean enabled;
       private String title;
       private List<String> actors;
    }
    

4. 测试配置项

⏰ 使用如下代码打印配置项
  • ⭕🌏 使用如下代码打印配置项
    @SpringBootApplication
    @Slf4j
    public class DemoPropertiesApplication implements ApplicationRunner {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoPropertiesApplication.class, args);
        }
    
        @Autowired
        private ApplicationProperty applicationProperty;
        @Autowired
        private TeleplayInfoProperty teleplayInfoProperty;
        @Override
        public void run(ApplicationArguments args) throws Exception {
            Dict set = Dict.create().set("applicationProperty", applicationProperty).set("blogInfoProperty", teleplayInfoProperty);
            log.info(new ObjectMapper().writeValueAsString(set));
        }
    }
    
⏰ 启动查看日志
  • ⭕🌏 如下可见激活了 dev,comm,tv 配置
    INFO 32 --- [           main] c.o.d.DemoPropertiesApplication          : The following profiles are active: dev,comm,tv
    
  • ⭕🌏 如下可见打印的配置项
     {
        "applicationProperty": {
            "name": "Spring Boot Demo",
            "version": "2.4.1"
        },
        "blogInfoProperty": {
            "website": "test.ohbee.com",
            "enabled": false,
            "title": "friends",
            "actors": ["Joey", "Phoebe", "Rachel", "Chandler", "Ross", "Monica"]
        }
    }
    

Hi~ o( ̄▽ ̄)ブ 🌱 源码地址

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值