首页 > 编程语言 > spring boot 使用profile来分区配置的操作
2021
10-12

spring boot 使用profile来分区配置的操作

spring boot 使用profile来分区配置

很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同的环境下配置用不同的配置文件或者不同的配置

spring boot允许你通过命名约定按照一定的格式(application-{profile}.properties)来定义多个配置文件,然后通过在application.properyies通过spring.profiles.active来具体激活一个或者多个配置文件,如果没有没有指定任何profile的配置文件的话,spring boot默认会启动application-default.properties。

profile的配置文件可以按照application.properyies的放置位置一样,放于以下四个位置,

  • 当前目录的 “/config”的子目录下
  • 当前目录下
  • classpath根目录的“/config”包下
  • classpath的根目录下

在这里我们就定义俩个profile文件,application-cus1.properties和application-cus2.properties,并在俩个文件中都分别写上变量cusvar=cus1和cusvar=cus2

这里写图片描述

我们在application.properyies也写上,并把profile切换到application-cus1.properties的配置文件

cusvar=cus3
spring.profiles.active=cus1

可以通过这样子来测试

@RestController
@RequestMapping("/task")
public class TaskController {
    @RequestMapping(value = {"/",""})
    public String hellTask(@Value("${cusvar}")String cusvar ){
        return "hello task !! myage is " + cusvar;
    }
}

在这里可以看到spring.profiles.active激活的profile不同,打印出来的结果也不一样。

除了可以用profile的配置文件来分区配置我们的环境变量,在代码里,我们还可以直接用@Profile注解来进行配置,例如数据库配置,这里我们先定义一个接口

public interface DBConnector {
    public void configure();    
}

分别定义俩个实现类来实现它

/**
  * 测试数据库
  */
@Component
@Profile("testdb")
public class TestDBConnector implements DBConnector {
    @Override
    public void configure() {
        System.out.println("testdb");
    }
}
/**
 * 生产数据库
 */
@Component
@Profile("devdb")
public class DevDBConnector implements DBConnector {
    @Override
    public void configure() {
        System.out.println("devdb");
    }
}

通过在配置文件激活具体使用哪个实现类

spring.profiles.active=testdb

然后就可以这么用了

@RestController
@RequestMapping("/task")
public class TaskController {
    @Autowired DBConnector connector ;
    @RequestMapping(value = {"/",""})
    public String hellTask(){
        connector.configure(); //最终打印testdb     
        return "hello task !! myage is " + myage;
    }
}

除了spring.profiles.active来激活一个或者多个profile之外,还可以用spring.profiles.include来叠加profile

spring.profiles: testdb
spring.profiles.include: proddb,prodmq

spring boot配置之Profile的使用

profile是Spring对不同环境提供不同配置功能的支持,可以通过激活

指定参数等方式快速切换环境

1. 多profile文件形式

  • 格式:application-{profile}.properties
  • 例如常见的:application-dev.properties, application-uat.properties, application-prod.properties

2. 多profile文档块模式

spring:
  profiles: dev
server:
  port: 8081
---
spring:
  profiles: uat
server:
  port: 8082
---
spring:
  profiles: prod
server:
  port: 8083

在这里插入图片描述

在这里插入图片描述

3. 激活方式

1.在默认配置文件application.properties中指定 spring.profiles.active=dev

在这里插入图片描述

application.properties文件
#激活application-dev.properties中的配置
spring.profiles.active=dev
application-dev.properties文件
server.port=8081

2.命令行

java -jar ch02-springboot-config-profile-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

3.虚拟机参数 常用

在"VM options"中添加-Dspring.profiles.active=dev

在这里插入图片描述

在这里插入图片描述

4.“Program atguments”

在“Program atguments”中添加?spring.profiles.active=dev

在这里插入图片描述

4. 不同位置的配置文件优先级

SpringBoot启动会扫描以下位置的application.properties/application.yml文件作为SpringBoot的默认配置文件

  • file:./config/
  • file:./
  • classpath:/config/
  • classpath:/

以上是按照优先级从高到低的顺序,所有问题的文件都会被加载。如果配置文件中存在相同配置,则高优先级配置内容会覆盖低优先级配置内容。SpringBoot会从这四个位置全部加载主配置文件生成一个互补配置的文件。还可通过spring.congif.location改变默认的配置文件位置

以下图中的优先级为1?>2?>3?>4, 不过1和2不会被打到包里

在这里插入图片描述

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自学编程网。

编程技巧