springboot启动环境变量profile maven打包profile内部原理

springboot启动处理步骤:
1、不接配置中心的情况下,启动的时候springboot默认会加载bootstrap.yml 以及 bootstrap- p r o f i l e {profile}在bootstrap.yml中 spring.profiles.active: dev 指定。

另外说一下,加载顺序如下: bootstrap.yml 》 bootstrap-dev.yml 》 application.yml 》
application-dev.yml
也就是说先加载bootstrap开头的再去加载application开头的;同为bootstrap开头的bootstrap.yml比bootstrap-dev.yml先加载,同为application开头的同理。
如果这4个配置文件中存在相同的属性,那么后加载的属性值会覆盖掉前加载的属性值。

测试代码如下:

@Slf4j
@Configuration
public class TestConfig {

    @Value("${test.p}")
    private String testProfile;

    @Bean
    public String test(){
        System.out.println("test.p="+testProfile);
        log.info("test.p="+testProfile);
        return "";
    }
}

bootstrap-dev.yml:

test.p: dev

bootstrap-test1.yml:

test.p: test

在idea中指定启动参数 spring.profiles.active=dev,相当于在java -jar -Dspring.profiles.active=dev
这里写图片描述
启动应用打印:
这里写图片描述
把启动参数改为test1,启动打印:
这里写图片描述
以上的操作证明了springboot在启动的时候只会加载指定的profile对应的bootstrap-profile文件,而不会加载未指定的profile文件。

2、有时候我们不希望在启动的时候才指定profile,例如不想在生产环境指定这个启动参数,那么这个时候我们可以在mvn构建的时候就做处理。处理的步骤是这样的:
①、父pom文件建立多个profile
这里写图片描述

<profiles>
        <profile>
            <id>sit</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>production</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
    </profiles>

②、多个子项目情况下,子pom文件使用maven逻辑指定过滤的properties文件,读取properties文件下的内容:
properties内容如下:

spring.profiles.active=test
spring.cloud.config.uri=xxx

这里会读取到这两个变量,spring.profiles.active和spring.cloud.config.uri
子项目pom文件如下设置:

<build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <filters>
            <filter>src/main/profiles/profile-${profiles.active}.properties</filter>
        </filters>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

其中profiles.active在maven打包的时候-P指定
读取到的配置spring.profiles.active和spring.cloud.config.uri会替换掉bootstrap.properties文件中的变量:
bootstrap.properties内容如下:

spring.application.name=jd-xx
spring.profiles.active=${spring.profiles.active}

spring.cloud.config.uri=${spring.cloud.config.uri}
spring.cloud.config.enabled=true

这里打包出来的结果是:

扫描二维码关注公众号,回复: 2970690 查看本文章
spring.application.name=jd-xx
spring.profiles.active=dev

spring.cloud.config.uri=xxx
spring.cloud.config.enabled=true

这样就在maven打包的时候指定了profile为dev,不需要再启动的时候再次指定profile了。指定了profile后,读取的文件原理同上,也是读取bootstrap.yml以及application-profile.yml或者bootatrap-profile.yml 。

完毕!

猜你喜欢

转载自blog.csdn.net/heweimingming/article/details/80069958