SpringBoot的Profiles根据开发环境和测试环境载入不同的配置文件

时间:2023-03-08 20:42:56

参考:https://www.cnblogs.com/bjlhx/p/8325374.html

1、需要有一个默认的配置文件,然后一个正式的配置文件,一个测试的配置文件。激活配置项,默认的配置文件application.properties也会加载进去的。编程的方式指定生效的profile。

默认的配置文件application.properties配置文件,然后再创建两个配置文件,一个是application-dev.properties,一个是application-test.properties配置文件。
SpringBoot的Profiles根据开发环境和测试环境载入不同的配置文件

application-dev.properties内容如下所示:

jdbc.drivername=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///book
jdbc.user=dev
jdbc.password=

application-test.properties内容如下所示:

jdbc.drivername=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///book
jdbc.user=test
jdbc.password=

然后在主运行类里面进行编程修改加载那个配置文件,具体如下所示:

 package com.bie;

 import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext; import com.bie.springboot.DataSourceProperties;
import com.bie.springboot.JdbcConfig;
import com.bie.springboot.TomcatProperties;
import com.bie.springboot.UserConfig; /**
*
* @Description TODO
* @author biehl
* @Date 2018年12月30日 上午10:44:35
*
*/
@SpringBootApplication
public class Application { public static void main(String[] args) {
//ConfigurableApplicationContext run = SpringApplication.run(Application.class, args); SpringApplication app = new SpringApplication(Application.class);
//使正式环境的配置文件生效
app.setAdditionalProfiles("dev");
//使测试环境的配置文件生效
//app.setAdditionalProfiles("test");
ConfigurableApplicationContext context = app.run(args); System.out.println("==================================================="); System.out.println("jdbc.drivername = " + context.getEnvironment().getProperty("jdbc.drivername"));
System.out.println("jdbc.url = " + context.getEnvironment().getProperty("jdbc.url"));
System.out.println("jdbc.user = " + context.getEnvironment().getProperty("jdbc.user"));
System.out.println("jdbc.password = " + context.getEnvironment().getProperty("jdbc.password")); System.out.println("==================================================="); // 运行结束进行关闭操作
context.close();
} }

运行效果如下所示:

SpringBoot的Profiles根据开发环境和测试环境载入不同的配置文件

如何在@SpringBootTest中动态地启用不同的profiles?

由于是新接触到SpringBoot框架,所以也是慢慢学的,刚开始一致无法识别到自己的dev或者test配置文件。百度了不少教程,但是并没有解决这个问题,后来发现是我

的启动参数配置的是之前的,所以把启动参数删除了就行了。

SpringBoot的Profiles根据开发环境和测试环境载入不同的配置文件

将上面的配置参数删除了,就可以实现你想要的了。马上试试吧,骚年。

2、也可以使用@Profile注解进行选择启动dev或者test。装配了三个bean。如果test被激活了或者dev被激活了才会装配下面对应的bean,执行对应的方法。起到相应的作用和效果。

 package com.bie.springboot;

 import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile; /**
*
* @Description TODO
* @author biehl
* @Date 2018年12月30日 下午5:40:18
* 启动对应环境时候生效
*/
@SpringBootConfiguration
public class MyConfig {
@Bean
public Runnable createRunnable() {
System.out.println("--------1--------");
return ()->{};
} @Bean
@Profile("dev")
public Runnable createRunnable2() {
System.out.println("--------2--------");
return ()->{};
} @Bean
@Profile("test")
public Runnable createRunnable3() {
System.out.println("--------3--------");
return ()->{};
}
}

待续......