# 08.SpringBoot-高级特性

# 1.Profile环境切换

为了方便多环境适配,Spring Boot简化了profile功能。

1.基本使用:

  • 激活指定环境
    • 配置文件激活:spring.profiles.active=prod
    • 命令行激活:java -jar xxx.jar --spring.profiles.active=prod --person.name=haha(修改配置文件的任意值,命令行优先

注意:

  • 同名配置项,profile配置优先(同时配置server.port)
  • 默认配置与环境配置同时生效 (主配置和选择的配置)
spring:
  profiles:
    active: prod
1
2
3

2.@Profile条件装配

public interface Person {

   String getName();
   Integer getAge();

}

@Profile("test")//加载application-test.yaml里的
@Component
@ConfigurationProperties("person")
@Data
public class Worker implements Person {

    private String name;
    private Integer age;
}

@Profile("prod")//加载application-prod.yaml里的
@Component
@ConfigurationProperties("person")
@Data
public class Boss implements Person {

    private String name;
    private Integer age;
}
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

controller

@Autowired
private Person person;

@GetMapping("/")
public String hello(){
    //激活了prod,则返回Boss;激活了test,则返回Worker
    return person.getClass().toString();
}
1
2
3
4
5
6
7
8

激活的配置

# 激活prod配置文件
spring.profiles.active=prod
1
2

prod和test配置

#prod 
person:
  name: prod-张三

server:
  port: 8000 


#test
person:
  name: test-张三

server:
  port: 7000
1
2
3
4
5
6
7
8
9
10
11
12
13
14

也可在方法上

class Color {
}

@Configuration
public class MyConfig {

    @Profile("prod")
    @Bean
    public Color red(){
        return new Color();
    }

    @Profile("test")
    @Bean
    public Color green(){
        return new Color();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

可以激活一组:

spring.profiles.active=production

spring.profiles.group.production[0]=proddb
spring.profiles.group.production[1]=prodmq
1
2
3
4

# 2.配置加载优先级

官方文档 - Externalized Configuration (opens new window)

1.外部配置源包括

  • java属性文件

  • yaml文件

  • 环境变量

  • 命令行参数

2.配置文件的查找位置

  • classpath 根路径

  • classpath 根路径下的config

  • jar包所在目录

  • jar包所在目录下的config

  • config包下的直接子目录(比如/config/v1,Linux版本生效)

3.配置文件加载顺序

  • 当前jar包内部的application.properties和application.yml。

  • 当前jar包内部的application-{profile}.properties 和 application-{profile}.yml。

  • 引用的外部jar包的application.properties和application.yml。

  • 引用的外部jar包的application-{profile}.properties和application-{profile}.yml。

4.指定环境优先,外部优先,后面的可覆盖前面的同名配置


# 3.自定义starter

1.创建两个springboot项目,hello-springboot-autoconfiguration和hello-springboot-starter

autoconfiguration中的依赖

<?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>4.0.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>hello-springboot-autoconfiguration</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-springboot-autoconfiguration</name>
    <description>hello-springboot-autoconfiguration</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
</project>
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

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>4.0.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>hello-springboot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-springboot-starter</name>
    <description>hello-springboot-starter</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>hello-springboot-autoconfiguration</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
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

2.在autoconfiguration编写如下配置

bean包

package com.demo.hello.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
    private String prefix;
    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}
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

auto包

package com.demo.hello.auto;

import com.demo.hello.bean.HelloProperties;
import com.demo.hello.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @ConditionalOnMissingBean(HelloService.class)
    @Bean
    public HelloService helloService(){
        return new HelloService();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

service包

package com.demo.hello.service;

import com.demo.hello.bean.HelloProperties;
import org.springframework.beans.factory.annotation.Autowired;

public class HelloService {
    @Autowired
    private HelloProperties helloProperties;

    public String sayHello(String username){
        return helloProperties.getPrefix()+":"+username+","+helloProperties.getSuffix();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

根目录下的META-INF/spring/factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.demo.hello.auto.HelloServiceAutoConfiguration
1
2

3.install到本地maven

4.接下来新建starter-springboot-test的springboot项目进行测试

依赖

<?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>4.0.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>springboot-starter-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-starter-test</name>
    <description>springboot-starter-test</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>hello-springboot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

配置

spring.application.name=springboot-starter-test
server.port=8888
hello.prefix=aa
hello.suffix=bb
1
2
3
4

controller

@RestController
public class helloController {
    @Autowired
    private HelloService helloService;
    @RequestMapping("/hello")
    public String hello(){
        return helloService.sayHello("lao6");
    }
}
1
2
3
4
5
6
7
8
9

结果:aa:lao6,bb


最近更新: 9/19/2026, 1:27:08 PM
编程NOTE   |