Spring boot 配置顺序、获取配置、切换不同的配置

测试源码下载:https://github.com/galen17/spring-bootProfile

项目中已经包含了:application.properties和application-aabb.properties,不改动直接运行时应用的application-aabb.properties

一、配置顺序

1、不同文件的优先级

application.yml

application.properties

按照优先级排序,yml大于properties

 

2、配置文件的优先级顺序,由高到底排序

  1. 外置,在应用程序运行的目录里的/config
  2. 外置,在应用程序运行的目录里
  3. 内置,在config包内
  4. 内置,在Classpath根目录

 

3、配置实践

需要的依赖

为了让 Spring Boot 更好的生成配置元数据文件,我们需要添加如下依赖(该依赖可以不添加,但是在 IDEA 和 STS 中不会有属性提示,没有提示的配置就跟你用记事本写代码一样苦逼,出个问题弄哭你去),该依赖只会在编译时调用,所以不用担心会对生产造成影响

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

 

3.1应用配置,并获得值

application.properties中添加配置

#应用的端口

server.port=9090

#根路径

server.servlet.context-path=/chapter1

#可以测试获取值
server.duorou=9900

server.zhoudao=buhouzhui

 

新建java

package com.battcn.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.stereotype.Component;

@Component

@ConfigurationProperties("server")

public class Properes1 {

    private Integer duorou;//对应application.properties中的server.duorou=9900
  private String zhoudao;

public String getZhoudao() {  
    return zhoudao;
}
public void setZhoudao(String zhoudao) {
    this.zhoudao = zhoudao;
}

public Integer getDuorou() {

return duorou;

}
public void setDuorou(Integer duorou) {
        this.duorou = duorou;
    }
}

 

 

com.battcn.ProfileController

package com.battcn;



import com.battcn.config.MyProperties2;

import com.battcn.config.Properes1;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;



@RequestMapping("/properties")

@RestController

public class ProfileController {

    private static final Logger log = LoggerFactory.getLogger(ProfileController.class);

    private final MyProperties2 myProperties2;

    private final Properes1 properes1;



    @Value("${server.zhoudao}")

    private String my1Name;



    @Autowired

    public ProfileController(Properes1 properes1, MyProperties2 myProperties2) {

        this.properes1 = properes1;

        this.myProperties2 = myProperties2;

    }



    @RequestMapping("/getProperties1")

    public void getProperties1() {

        Integer duorou = properes1.getDuorou();



        String zhoudao = properes1.getZhoudao();

        System.out.println("properties:" + duorou);

        System.out.println("buhouzhuidezhi:" + zhoudao);

    }



    @RequestMapping("/getProperties2")

    public MyProperties2 getProperties2() {

        log.info("=================================================================================================");

        log.info(myProperties2.toString());

        log.info("=================================================================================================");

        return myProperties2;

    }

}

 

运行后,调用:http://localhost:9090/chapter1/properties/getProperties1

在控制台就能打印出:properties:9900

 

4、切换不同配置文件,不同环境对应的配置文件prolife

例如源码中的application.properties和application-aabb.properties

假如application.properties是生产环境,application-aabb.properties是开发环境;如何在切换不同的配合文件?

application.properties中添加:

spring.profiles.active=aabb

这样就是制定了application-aabb.properties,其中aabb是自己设定的后缀,格式为:application-{自己设定的后缀}.properties

添加application-aabb.properties

server.servlet.context-path=/aabb
server.port=8080

server.duorou=5200

这样运行后就不会用application.properties,而会应用application-aabb.properties。

但是只会应用两者相同的配置,application.properties中比application-aabb.properties多的配置还会应用application.properties

 

4.1、打成Java包的运行方式

  1. 运行:java -jar chapter1-0.0.1-SNAPSHOT.jar

此时会应用application-aabb.properties

  1. java -jar chapter1-0.0.1-SNAPSHOT.jar --spring.profiles.active=false

此时会应用application.properties

  1. 在jar包在的目录中添加properties文件

再执行java -jar chapter1-0.0.1-SNAPSHOT.jar

此时应用目录中的properties文件

  1. 在jar包在的目录中添加config文件夹,并在其中添加properties文件

再执行java -jar chapter1-0.0.1-SNAPSHOT.jar

此时应用config文件夹中的properties文件

结论:由此可确定应用配置顺序,在我们现在开发的项目中,配置肯定要放着jar外面方便修改。就要用上面的3和4的方式

 

二、获取配置中的内容

1、上面Properes1实例就已经获取了application.properties中的内容;

 

2、获取其他的配置文件

my2.properties

my2.age=22
my2.name=Levin
my2.email=1837307557@qq.com

 

Myproperties2.java文件

package com.battcn.config;



import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.PropertySource;

import org.springframework.stereotype.Component;



/**

 * @author Levin

 * @since 2018/4/23 0023

 */

@Component

@PropertySource("classpath:my2.properties")

@ConfigurationProperties(prefix = "my2")

public class MyProperties2 {

    private int age;

    private String name;

    private String email;

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }



    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getEmail() {

        return email;

    }

    public void setEmail(String email) {

        this.email = email;

    }

    @Override

    public String toString() {

        return "MyProperties2{" +

                "age=" + age +

                ", name='" + name + '\'' +

                ", email='" + email + '\'' +

                '}';

    }

}

 

这样就Myproperties2和my2.properties就是会一一对应,项目启动就会进行实例化。

运行项目:调用http://localhost:8080/aabb/properties/getProperties2

就会打印出这个配置文件的内容。

 

3、用@Value就能获得application.properties中相应的值,

类似ProfileController

@Value("${server.zhoudao}")
private String my1Name;

 

其他

# 在配置文件中获取随机数配置随机值,一般情况下用不着,此处不做详细赘述,有兴趣自己看看 RandomValuePropertySource 就可以了
#my.secret=${random.value}
#my.number=${random.int}
#my.bignumber=${random.long}
#my.uuid=${random.uuid}
#my.number.less.than.ten=${random.int(10)}
#my.number.in.range=${random.int[1024,65536]}
posted @ 2021-04-11 19:08  五星上酱程序员  阅读(269)  评论(0编辑  收藏  举报