zoukankan      html  css  js  c++  java
  • 补习系列(10)-springboot 之配置读取

    目录

    简介

    在早前的博客中曾经写过 Spring 程序通过 Bean 映射实现配置信息的读取。
    在SpringBoot 框架中读取配置的方式变得非常多样,这导致读者在搜寻资料时反而容易迷糊。

    • 到底,SpringBoot 是按什么顺序加载配置?
    • 相应的,我们该选择什么样的方式去读取?

    一、配置样例

    先看一个例子:

    @Compoment
    public class BuildConfig{
    
       @Value("${buildinfo.version")
       private String version;
    
      ...
    }

    代码中,@Component 将 BuildConfig 注册为 Bean ,
    接下来使用 @Value 注解,将 配置中的 buildinfo.version键映射到了 version 字段上。

    我们都知道,通过 application.properties 可以方便的配置一些属性。
    属性的值是支持变量替换的,如下:

    myName=Lilei
    myDesc=${myName} is a good man 

    这点,是由 SpringBoot 自动生成的 PropertyPlaceholderConfigurer 对象实现的。

    除了 上面所说 application.properties 之外,还有什么途径?
    下面介绍如何注入配置

    二、如何注入配置

    1. 缺省配置文件

    类路径中 application.properties(yml) 是默认的配置文件。
    此外如果启动应用时,当前目录中存在同名的配置文件,则以此优先。

    在此规则之下,SpringBoot 还能识别不同 profile下的配置,这将在后面篇幅中介绍。

    2. 使用注解

    @PropertySource

    可指定属性配置文件的位置,
    样例代码:

    @Configuration
    
    @PropertySource("classpath:/com/myco/app.properties")
    
    public class AppConfig {
    
         @Autowired
    
         Environment env;
    
    
    
         @Bean
    
         public TestBean testBean() {
    
             TestBean testBean = new TestBean();
    
             testBean.setName(env.getProperty("testbean.name"));
    
             return testBean;
    
         }
    
    }

    @TestPropertySource

    与 @PropertySource 类似,该注解用于指定测试环境中的属性文件,其优先级高于 @PropertySource。

    3. 启动参数

    以下的命令以指定参数启动 SpringBoot 应用

    java -jar application.jar --server.port=9000

    server.port 值将被注入为环境属性值。

    而以下的命令还可以指定 配置文件的位置

    java -jar application.jar --spring.config.location=/etc/xxx.properties

    这个spring.config.location就是指的配置文件位置,
    默认情况下,SpringBoot 会从下面几路径找到配置文件:

    路径
    file:./config/
    file:./
    classpath:/config/
    classpath:/

    还有..

    SpringBoot 注入配置的方式其实非常多,完整顺序如下表:

    优先级 配置
    1 @TestPropertySource 注解
    2 @SpringBootTest 注解
    3 命令行参数
    4 SPRING_APPLICATION_JSON 属性值(或环境变量)
    5 Servlet 相关参数
    6 JNDI 属性
    7 Java 系统属性 (System.getProperties())
    8 操作系统环境变量
    9 RandomValuePropertySource 随机属性
    10 Jar包外部 application-{profile}.properties
    11 Jar包内部 application-{profile}.properties
    12 Jar包外部 application.properties
    13 Jar包内部 application.properties
    14 @PropertySource 注解
    15 SpringApplication 默认值

    三、如何读取配置

    @Value 注解

    如以下的实现:

    @Configuration
    public class AppConfig {
        
        @Value("${api.log.enabled:false}")
        private boolean apiLogEnabled;

    除了类型自动转换之外,通过:false后缀可以指定默认值。

    Environment 接口

    Environment 是一个类似 Properties 的接口,用来获取属性非常方便。

    @Configuration
    public class AppConfig {
    
        @Autowired
        private Environment environment;
    
        public String getApplicationId() {
            return this.environment.getProperty("application.id");
        }
    }

    @ConfigurationProperties 注解

    该注解一般用作前缀匹配,下面的代码摘自Mongodb

    @ConfigurationProperties(prefix = "spring.data.mongodb")
    public class MongoProperties {
    
     /**
      * Mongo server host.
      */
     private String host;
    
     /**
      * Mongo server port.
      */
     private Integer port = null;
    
     /**
      * Database name.
      */
     private String database;

    相应的 Mongodb 配置信息如:

    spring.data.mongodb.host=127.0.0.1
    spring.data.mongodb.port=27017
    spring.data.mongodb.database=xxx

    四、不同环境中的配置

    Spring 提供了 Profile 机制用于管理不同环境的配置。

    配置内容可以是 Java Config(对应@Component或@Configuration),也可以是配置文件。
    如:

    @Configuration
    @Profile("prod")
    public class ProdConfiguration {
    
     // ...
    
    }

    通过@Profile注解可将代码配置关联到某个配置环境

    在具体应用中,Profile的用途通常有二:

    1. 区别开发、测试、发布环境

    对于dev、prod、test分别做不同的配置

    //for dev
    application-dev.properties
    
    //for prod
    application-prod.properties
    
    //for test
    application-test.properties

    可以在 application.properties 指定启用的环境:

    spring.profiles.active=dev

    也可以通过命令行指定:

    java -jar app.jar --spring.profiles.active=prod

    2. 声明多配置文件

    当内容过多时,可以将配置信息进行拆分,如下:

    application-mongodb.properties

    spring.data.mongodb.host=127.0.0.1
    spring.data.mongodb.port=27017
    spring.data.mongodb.username=xxx
    spring.data.mongodb.password=xxx
    spring.data.mongodb.database=xxx

    application-mail.properties

    spring.mail.host=xxx
    spring.mail.username=xxx
    spring.mail.password=xxx
    
    spring.mail.from=xxx
    spring.mail.to=xxx
    spring.mail.cc=xxx

    在主配置文件指定包含关系:

    application.properties

    spring.profiles.include=mongodb,mail

    参考文档

    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

    欢迎继续关注"美码师的补习系列-springboot篇" ,如果觉得老司机的文章还不赖,请多多分享转发^-^

    作者:美码师

  • 相关阅读:
    System.DateUtils 1. DateOf、TimeOf 简单修饰功能
    Servlet高级
    Servlet基础
    AOP的基本概念
    Bean的定义及作用域的注解实现
    Bean
    centos7系统下,配置学校客户端网络记录
    CUDA并行编程思维过程
    CUDA(GPU)OPENCV
    3-决策树
  • 原文地址:https://www.cnblogs.com/2020-zhy-jzoj/p/13165545.html
Copyright © 2011-2022 走看看