Spring 设置profile (Maven方式)

Spring 设置profile (Maven方式)

在开发环境,测试环境,生产环境 ,我们很多配置是不同的,如果我们每次都为了测试或者生产而改变配置,那就很麻烦! 又或者你要通过jenkins拉取git或者svn的代码,那配置简直是无法修改的.所以针对这些不同的环境 我们需药灵活的切换配置

1. SSM 设置profile

增加profile配置

<profiles>
    <!-- 开发环境,默认为开发环境 -->
    <profile>
        <id>dev</id>
        <properties>
            <envName>dev</envName>
            <activeProfile>dev</activeProfile>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <!-- 测试环境 -->
    <profile>
        <id>test</id>
        <properties>
            <envName>test</envName>
            <activeProfile>test</activeProfile>
        </properties>
    </profile>
</profiles>
  • profile : 上面配置了两个环境 (dev和test),也可以配置很多很多很多个
  • activeByDefault : 默认的开发环境
  • properties : 此处为maven构建时的参数,众所周知,项目在构建的时候,一定是maven先执行,其次才是spring ..等框架执行代码 .所以只要在编译maven前替换一些参数,那么环境切换也就不是难事.

增加build配置

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/webapp</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
                <includeEmptyDirectories>true</includeEmptyDirectories>
            </configuration>
        </plugin>
    </plugins>
</build>   
  • resource : 构建的资源,可以有多个 , : 资源目录.:需要做propertie参数替换的过滤配置
  • maven-resources-plugin : maven构建时资源插件.很好用~功能很全~
  • maven-war-plugin : maven构建war包插件
  • filteringDeploymentDescriptors :过滤时需要构建资源提示符,这里指的就是web.xml
  • includeEmptyDirectories : 是否构建空文件夹

参数使用

springmvc中,如果需要扫描一些proerties配置,肯定会包含<context:property-placeholder location="classpath*:*properties" /> , 那么我们试想,我能不能把它做两套配置,放到不同目录,然后我通过变量切换目录呢?

那么当然是可以的!

我们可以这么做 :

  • 首先建立一个env的文件夹 ,其中包含devtest文件夹,这两个文件夹包含项目的配置文件
  • 在配置文件(文件一定包含在标签配置的目录中,否则不会使用过滤)中修改扫描properties配置 . i.e. <context:property-placeholder location="classpath*:env/$/*.properties" />

上述完成了配置参数的profile还不够,spring的profile还是没有修改的,下面我们去修改它

首先设置spring的profile环境,需要在web.xml中增加

<!-- 设置spring profile,activeProfile为maven替换参数 -->
    <context-param>
	    <param-name>spring.profiles.active</param-name>
	    <param-value>dev</param-value>
	</context-param>

那么刚才我们有一个properties的配置参数是activeProfile,我们直接修改为下面的代码即可 :

<!-- 设置spring profile,activeProfile为maven替换参数 -->
    <context-param>
	    <param-name>spring.profiles.active</param-name>
	    <param-value>${activeProfile}</param-value>
	</context-param>

这里必须增加<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>配置,不然在项目中web.xml是不会替换变量的.

2. springboot设置profile

springboot中设置profile就简单多了,而且功能强大

springboot默认使用application.properties配置文件。我们只需要增加:

application-dev.propertiesapplication-test.properties两个配置文件.

分别增加spring.profiles = dev, spring.profiles = test

spring.profiles.active = dev用来配置需要默认使用的文件.

注意:如果spring.profiles.active没有指定值,那么只会使用没有指定spring.profiles文件的值,也就是只会加载通用的配置。
在运行时可以在随后增加参数 -spring.profiles.active=dev来切换配置


Spring 设置profile (Maven方式)
https://www.blaaair.com/archives/springshe-zhi-profilemaven-fang-shi-
作者
Glo6f
发布于
2022年01月27日
许可协议