# spring boot高级特性
# profile
有时候,我们的生产环境和实际环境可能不一样,那么这个时候,就可以使用profile的特性,快速的就可以进行切换
# application-profile功能
默认配置文件 application.properties;任何时候都会加载
指定环境配置文件 application-{env}.yaml
激活指定环境
配置文件激活
命令行激活:java -jar xxx.jar --spring.profiles.active=prod --person.name=haha
修改配置文件的任意值,命令行优先
默认配置与环境配置同时生效
同名配置项,profile配置优先
其中application-test.yml
是测试环境,application-pro.yml
是生产环境
因为application.properties
和application.yml
是一定会加载的,所以我们可以在这两个中的其中一个进行选择,
spring.profiles.active=test # 使用测试环境的配置,也就是application-test.yml
成功被切换
# 自定义starter
我们可以自定义自己的starter
像官方的部分starter,他们当中,并没有代码,其作用就只是引入其他的jar文件,所以我们也可以依照这个来做
autoconfigure包中配置使用 META-INF/spring.factories 中 EnableAutoConfiguration 的值,使得项目启动加载指定的自动配置类
编写自动配置类 xxxAutoConfiguration -> xxxxProperties
@Configuration
@Conditional
@EnableConfigurationProperties
@Bean
......
引入starter --- xxxAutoConfiguration --- 容器中放入组件 ---- 绑定xxxProperties ---- 配置项
# 自定义starter
atguigu-hello-spring-boot-starter(启动器)
atguigu-hello-spring-boot-starter-autoconfigure(自动配置包)
# 官方starter分析
官方真正的配置,如果代码少量的话,那么可以直接使用一个包
# 自动配置
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {
@ConditionalOnMissingBean(HelloService.class)
@Bean
public HelloService helloService() {
return new HelloService();
}
}
2
3
4
5
6
7
8
9
10
@ConfigurationProperties("qsyyke.hello")
public class HelloProperties {
/** 这是前缀 */
private String Prefix;
/** 这是后缀 */
private String suffix;
public String getPrefix () {
return Prefix;
}
public void setPrefix (String prefix) {
Prefix = prefix;
}
public String getSuffix () {
return suffix;
}
public void setSuffix (String suffix) {
this.suffix = suffix;
}
@Override
public String toString () {
return "HelloProperties{" + "Prefix='" + Prefix + '\'' + ", suffix='" + suffix + '\'' + '}';
}
}
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
public class HelloService {
@Autowired
private HelloProperties properties;
public String sendMsg(String username) {
if (StringUtils.isEmpty(username)) {
username = "";
}
return properties.getPrefix() + " --- " + username + " --- " + properties.getSuffix();
}
@Override
public String toString () {
return "HelloService{" + "properties=" + properties + '}';
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
自动配置依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>vin.cco</groupId>
<artifactId>qsyyke-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>qsyyke-spring-boot-starter-autoconfigure</name>
<description>this is my first strter of autoconfigure</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
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
- spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
vin.cco.qsyyke.HelloAutoConfiguration
2
3
# 空壳starter
依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>vin.cco</groupId>
<artifactId>qsyyke-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>qsyyke-spring-boot-starter</name>
<description>this is my first starter</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>vin.cco</groupId>
<artifactId>qsyyke-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
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
# 测试
- 依赖
<dependency>
<groupId>vin.cco</groupId>
<artifactId>qsyyke-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2
3
4
5
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(HelloService service) {
System.out.println(service);
String chuchen = service.sendMsg("chuchen");
return chuchen;
}
}
2
3
4
5
6
7
8
9
10
qsyyke.hello.Prefix = hello
qsyyke.hello.suffix = thank
2