14.profiles配置详解

 

1,为什么要使用profiles

在开发中,一般有多种环境,例如:

  1. 生产环境
  2. 开发环境

有时候开发环境和生产环境的配置方法是不一样的,那么如何快速的切换呢,这里就要使用profiles文件

2. 使用方法

1,创建applicatin-dev.properties

1
server.port=8010

2,创建applicatin-pro.properties

1
server.port=8020

3,修改application.properties

1
2
#server.port=8080
spring.profiles.active=prod

spring.profiles.active=dev:

1
2021-03-25 12:13:36.722  INFO 11064 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8010 (http) with context path ''

spring.profiles.active=prod:

1
2021-03-25 12:12:28.874  INFO 10972 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8020 (http) with context path ''

application.properties即使设置了server.port=8080,激活dev/prod,最终加载的是dev/prod的配置。

3. 注解使用方法

DataSource.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.example.demo.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class DataSource {

private String driverClassName;
private String url;
private String username;
private String password;

}

DataSourceConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import com.example.demo.vo.DataSource;

@Configuration
public class DataSourceConfig {

@Bean
@Profile(value="dev")
public DataSource getDataSourceDev() {
DataSource dataSource = new DataSource();

dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("123456");

return dataSource;

}

@Bean
@Profile(value="prod")
public DataSource getDataSourceProd() {
DataSource dataSource = new DataSource();

dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://192.168.10.132:3306/ping?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("dv*3s;Qdc]p");

return dataSource;

}
}

application.properties

1
2
3
4
#server.port=8090

#spring.profiles.active=dev
spring.profiles.active=prod

ApplicationTests.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.example.demo.vo.DataSource;

@SpringBootTest
class ApplicationTests {

@Autowired
private DataSource dataSource;

@Test
void contextLoads() {

System.out.println(dataSource);
/*
1. spring.profiles.active=dev:

DataSource(driverClassName=com.mysql.cj.jdbc.Driver, url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC, username=root, password=123456)

2. spring.profiles.active=prod:
DataSource(driverClassName=com.mysql.cj.jdbc.Driver, url=jdbc:mysql://192.168.10.132:3306/ping?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC, username=root, password=dv*3s;Qdc]p)
*/
}
}

4. 去掉application.properties的运行方式

项目右键->Run As->Run Configuartions...

在VM arguments输入:

1
-Dspring.profiles.active=dev
1
2021-03-25 12:19:20.959  INFO 6148 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8010 (http) with context path ''

5. 去掉application.properties的jar包运行方式

用maven把项目打成jar包,

1
java -jar 04_springboot_config_profiles_my-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod

output:

1
2021-03-25 12:45:51.352  INFO 14208 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8020 (http) with context path ''

6. yml文件的多配置块的集成切换

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
spring:
profiles:
active:
- dev
server:
port: 8090

---

spring:
profiles: dev
server:
port: 8010

---

spring:
profiles: prod
server:
port: 8020
1
2
3
4
5
6
7
# spring:
# profiles:
# active:
# - dev

# output: 2021-03-29 12:24:45.723 INFO 10580 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8090 (http) with context path ''

1
2
3
4
5
6
spring:
profiles:
active:
- dev

# output: 2021-03-29 12:28:09.479 INFO 10580 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8010 (http) with context path ''
1
2
3
4
5
6
spring:
profiles:
active:
- prod

# output: 2021-03-29 12:28:56.948 INFO 10580 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8020 (http) with context path ''