Spring Boot Series (2) - configuration-related

Billion, Spring Boot global configuration file

  By previous we can know, when building a Spring Boot project, Spring Boot project has been the default configuration. However, in practice, each project has its own peculiarities, it is impossible to use only the default configuration, the need to add or modify some of its own configuration.

  Spring Boot configuration file has two formats:

    ->application.properties

    -> application.yml (or .yaml) features and syntax

  The name of the global configuration file (application.xxx) is fixed, the suffix can choose one. Like two formats to configure effect, but the syntax is slightly different.

 

First, change the default configuration

  1.Spring Boot default configuration of all items: https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#core-properties

  2. When we want to change one of these configuration items, you can just write directly overwrite the default configuration of our project global configuration file;

    For example: Modify the server port number (default is 8080, now revised to 8081)

    .yml file written (left) .properties file written (Right)

                         

 

 

    When re-start the program, you can see the port number has changed  

    

 

Second, get a custom configuration - Use @ ConfigurationProperties

  When we create a Java bean and hope that through their property profile assignment:

  1. Write Java bean

  

 

  2. Parameter Configuration (note name corresponds to support loosely binding syntax):

 

  

 

 

 

   3. Among them, the need to add notes in front of the class:

1 /**
2  * @ConfigurationProperties->将本来属性与配置文件中相关的参数绑定
3  * prefix->表示前缀
4  * @ConfigurationProperties只能对在Spring容器的类起作用
5  * @Component->表示Spring容器组件
6  */
7 @Component
8 @ConfigurationProperties(prefix = "people")

 

  4.@ConfigurationProperties需要导入配置文件解析器依赖

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-configuration-processor</artifactId>
     <optional>true</optional>
</dependency>

 

  5.测试运行

  

 

  

 

三、获取自定义配置——使用@Value

  1.使用@Value注解,其实就像Spring xml配置中的value=" "

<bean id=”” class=””>
   <property name=”属性名” value=”值”></property>
</bean>

  

  2.在Java bean属性前加上@Value("配置参数名")

  

 

 

  3.测试运行

   

  

 

  

 

  4.@ConfigurationProperties与@Value对比

   

 

 

四、引入自定义配置文件

  @ConfigurationProperties与@Value注解的配置,都是默认从全局配置文件(application.properties/.yml)中获取值。当我们太多参数要配置,需要一个额外的配置文件来写参数时,可以用以下方法配置。

  1.使用@PropertySource注解引入指定配置文件

    (1)创建自定义的配置文件,内容格式如同上面所谈

    

 

 

 

     (2)在需要配置的类前加上注解

    注意:若全局配置文件中的参数与自定义配置文件中的参数相同,优先使用全局配置文件的参数

    

 

 

五、为容器添加组件(bean)

  1.xml配置方式(早期spring风格)

    (1)用xml文件配置<bean>...</bean>    (2)在启动类上用@ImportResource注解引入该xml文件

  

  2.注解配置方式(配置类)

    (1)若在一个类前加上@Configuration注解,则表示这个类为配置类

    (2)在配置类的方法前加上@Bean注解,则表示该方法为一个 Java bean,并将该bean加入Spring容器中(bean名为方法名)

    

    (3)测试运行

    

 

 

 六、配置文件切换

  当我们开发阶段和生产阶段使用的配置参数不一样,又不想频繁将文件中的参数改来改去。我们可以同时创建多份同参数不同值的自定义配置文件,并在全局配置文件中指定使用哪一份。

  1.profile切换(.properties文件)

    方法:可以新建多个application-{profile标识}.properties/.yml,在全局配置文件中用'spring.profiles.active=profile标识' 指定当前所用配置文件。

    (1)分别新建profile文件并添加配置:

    application-development.properties——>配置端口号为8082,

    application-produce.properties——>配置端口号为8083

    在全局配置文件application.properties中填写spring.profiles.active=development

    

    (2)启动程序

    

 

 

  2.profile切换(yaml多文档块)

    方法:在全局配置文件application.yml中,用‘---’(三个小横杠)切分文档

    注意:当.yml配置文件和.properties文件并存时,优先.properties文件生效

    

 

 

  3.配置文件切换(命令行参数)

    java -jar jar包路径 --spring.config.location=配置文件路径

   

七、配置文件优先级顺序

  官网文档:https://docs.spring.io/spring-boot/docs/2.0.0.RC1/reference/htmlsingle/#boot-features-external-config

  注意:各文件不是独立生效,而是若参数相同,则按优先级;若参数不同,则各文件互补。

  

Guess you like

Origin www.cnblogs.com/Drajun/p/12142742.html