profile 能让maven项目在不同的环境下加载不同的配置,在pom文件中加入(如果是多模块项目,加在父POM中即可)
<profiles>
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
现在在resources目录下创建多个配置文件:
image.png
在resource标签内,可以指定加载的配置文件:
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application-${profileActive}.properties</include>
</includes>
<excludes>
</excludes>
</resource>
另外代码里,也可指定要加载的配置:
@PropertySource(value = {"classpath:service.properties","classpath:service-${spring.profiles.active}.properties"})
网友评论