实现方案:
主要实现思路是通过spring3.0之后提供的profile实现的,在springboot可以通过配置系统参数 spring.profiles.active 来实现加载指定配置文件。下面来贴几张项目结构及,部分服务器配置来加以说明
服务器配置图:
这里我们指定系统参数 --spring.profiles.active = common
下面是我们的配置文件目录及文件名:
接下来是我们的默认启动加载配置文件application.yml的配置
spring:
profiles:
include: jdbc
大家也可以把jdbc这个配置放到多环境配置隔离中(一般情况下jdbc配置,不同环境都是不同的,这里就没加入到多环境配置里面去了)
最后启动之后我们加载的资源文件就有:
application.yml
application-common.yml
application-jdbc.yml
这个时候如果我们要加入appliaction-common_pro.yml文件只需要将系统参数改为 --spring.profiles.active = common_pro,这样就能将加载的application-common.yml更改为appliaction-common_pro.ym文件了。
之后就是咱们的多环境测试类如何编写了:直接贴代码了,主要是通过@SpringBootTest注解提供的properties参数来设置启动加载配置文件;
package com.common.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.security.auth.login.Configuration;
@RunWith(SpringRunner.class)
@SpringBootTest(properties = {"spring.profiles.active=common"})
public class TestApplicationTests {
@Test
public void contextLoads() {
}
}
配置和上面基本一致,就不再说明了