spring @Profile注解

14 @Profile 根据不同环境初始化bean

我们知道实际工作中一般都会有本地开发环境(一般开发环境都等同于dev环境),uat测试环境(test),生产(pro)环境,不同的环境都需要读取不同环境的配置。本篇博客就讨论实际工作中springboot多环境配置管理。

比如我不同的环境定义了不同环境的配置文件

开发环境,application.properties:

1
spring.redis.host=192.168.1.111

测试环境,application-test.properties:

1
spring.redis.host=172.168.1.121

生产环境,application-pro.properties:

1
spring.redis.host=10.10.1.121

启动类:

1
2
3
4
5
6
7
8
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context =SpringApplication.run(Application.class,args);
String redishost = context.getEnvironment().getProperty("spring.redis.host");
System.out.println(redishost);
}
}

启动打印:

我们发现默认读的配置文件是application.properties,那么我们要去读取开发或者生产环境的配置文件呢?
有多种方式,

14.1 第一种,比如在application.properties中配置属性:

1
2
spring.redis.host=192.168.1.111
spring.profiles.active=test

发现已经读取到测试环境的配置。

14.2 第二种方式,启动项目的时候指定参数

idea配置页面,exclipse同理。

14.3 第三种,也可以在代码里配置,硬编码(不推荐)

1
2
3
4
5
6
7
8
9
10
@SpringBootApplication
public class Application2 {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
`springApplication.setAdditionalProfiles("test");`
ConfigurableApplicationContext context =springApplication.run(args);
String redishost =context.getEnvironment().getProperty("spring.redis.host");
System.out.println(redishost);
}
}

14.4 第四种打包环境,打包时指定

1
java -jar springboot-profile2-1.0-SNAPSHOT.jar --spring.profiles.active=pro

当然上面都可以指定多个profile文件,实际开发中很少这么做。
定义开发的application.properties

1
2
spring.redis.host=192.168.1.111
spring.profiles.active=test,pro

测试环境的application-test.properties:

1
2
spring.redis.host=172.168.1.121
spring.redis.database=0

生产中的application-pro.properties

1
2
spring.redis.host=10.10.1.121
spring.redis.password=323dsds32e3

1
2
3
4
5
6
7
8
9
10
11
12
@SpringBootApplication
public class Application3 {
public static void main(String[] args) {
ConfigurableApplicationContext context =SpringApplication.run(Application3.class,args);
String redishost = context.getEnvironment().getProperty("spring.redis.host");
String redisdatabase = context.getEnvironment().getProperty("spring.redis.database");
String redispassword = context.getEnvironment().getProperty("spring.redis.password");
System.out.println(redishost);
System.out.println(redisdatabase);
System.out.println(redispassword);
}
}

发现即读到测试的配置又读到生产的配置,测试,生产都有的配置取后配置的,比如我这边配置的

1
spring.profiles.active=test,pro

测试,生产都有spring.redis.host的配置,但是系统读到的是生产的配置。

根据不同的环境初始化不同的bean

14.5 定义一个普通的配置类MyConfig,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Configuration
public class MyConfig {

@Bean
public Runnable createDevRunnable(){
System.out.println("========dev========");
return () -> {};
}

@Bean
public Runnable createTestRunnable(){
System.out.println("========test========");
return () -> {};
}

@Bean
public Runnable createProRunnable(){
System.out.println("========pro========");
return () -> {};
}
}

启动执行类

1
2
3
4
5
6
7
@SpringBootApplication
public class Application4 {
public static void main(String[] args) {
ConfigurableApplicationContext context =SpringApplication.run(Application4.class,args);
System.out.println(context.getBeansOfType(Runnable.class));
}
}

发现三个类都初始化了,

需求这样,不同的环境初始化不同环境的bean,使用 @Profile注解,
配置类修改如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Configuration
public class MyConfig {

@Bean
@Profile("dev")
public Runnable createDevRunnable(){
System.out.println("========dev========");
return () -> {};
}

@Bean
@Profile("test")
public Runnable createTestRunnable(){
System.out.println("========test========");
return () -> {};
}

@Bean
@Profile("pro")
public Runnable createProRunnable(){
System.out.println("========pro========");
return () -> {};
}
}

注意:@Profile也可以修饰类,和修饰方法一致,这边就不多做过多解释了


本文转自:简书 - 二月_春风