🍞Spring Profiles

229 阅读3分钟

參考:

maven-profiles

www.baeldung.com/spring-prof…

maven.apache.org/guides/intr…

profile表示环境的概念,spring framework 提供了对 profile的支持,允许我们将 Bean 映射到不同的环境,比如:dev(开发)、test(测试)、prod(生产);

1、@profile 注解

`@profile`注解可以将bean 映射到不同的环境,比如说我们一个在 dev 开发环境下使用的数据源配置 bean :
@Component
@Profile("dev")
public class DevDatasourceConfig

这样DevDatasourceConfig 这个 bean 只会在开发环境下被注入 bean 容器。

@profile注解也支持 not 运算符,当我们想指定一个 bean 不在 dev 环境下时被注入,可以使用

@Component
@Profile("!dev")
public class DevDatasourceConfig

2、设置 profiles

有多种方式可以设置当前 profiles。

2.1 通过_**web.xml 设置**_

在 java Web 应用中,可以通过 web.xml 中context parameter 设置:
<context-param>
  <param-name>spring.profiles.active</param-name>
  <param-value>dev</param-value>
</context-param>

2.2 通过WebApplicationInitializer

`WebApplicationInitializer` 是 Spring 提供的一个接口,用于在基于 Servlet API 的 java Web 应用程序中以编程方式配置 ServletContext。

它允许开发者在应用启动时动态地设置 Servlet 容器的各种参数,而不需要依赖传统的 web.xml 配置文件。

@Configuration
public class MyWebApplicationInitializer 
  implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
 
        servletContext.setInitParameter(
          "spring.profiles.active", "dev");
    }
}

2.3 JVM 参数

在项目启动时,通过 JVM 系统参数也可以设置 profile:

-Dspring.profiles.active=dev

2.4 环境变量

也可以通过操作系统的环境变量来设置 profile ,比如说在 unix 系统中:

export spring_profiles_active=dev

2.5 maven profile

spring profile 也可以通过 maven profile 进行设置。

💡 扩展知识:www.baeldung.com/maven-profi…

比如在 pom.xml 中配置如下 profiles 属性:

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <spring.profiles.active>dev</spring.profiles.active>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <spring.profiles.active>prod</spring.profiles.active>
        </properties>
    </profile>
</profiles>

上述 profiles 属性配置了两个 profile属性,id 为devprod,在 profile的 properties 属性中设置spring.profiles.active属性。

这段配置的作用是当我们激活某个 maven profile 时,相应的spring.profiles.active属性值会替换application.properties 中的${spring.profiles.active}的占位符。

我们还需要在 pom.xml 中配置:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    ...
</build>

这样配置后,我们可以在 maver cli 命令中添加-P参数来切换不同的 profiles。

<font style="color:rgb(0, 0, 0);">mvn clean package -Pprod</font>

当我们使用 IDEA 进行开发时,还可以使用 IDEA 内置的 maven 插件界面切换 profile,

2.6 不同方式的优先级

上边我们列出了 5 中设置 profile 的方式,这些方式生效的优先级从高到低位:
  1. web.xml 设置Context parameter
  2. WebApplicationInitializer
  3. JVM 系统参数
  4. 环境变量
  5. Maven profile

高优先级的设置会覆盖低优先级。

3、default profile

对于没有指定 profile 的所有 spring bean 属于`default`profile。

我们也可以通过设置spring.profiles.default属性来修改默认的 profile

4、spring boot profile

spring boot 提供了对基于 profile 的配置文件的支持,可以对不同的环境(如开发、测试、生产)创建独立的配置文件,这些文件的命名格式为:

application-{profile}.properties

比如:

  • application-dev.properties:用于开发环境的配置。
  • application-prod.properties:用于生产环境的配置。

注意,application.yml 是全局配置文件,会在所有环境下默认加载。

而特定环境配置文件中的配置会覆盖掉全局配置文件中的同名配置。

application.yml中可以设置当前激活的 profile:

spring:
  profiles:
    active: dev

参考

[maven-profiles](https://www.baeldung.com/maven-profiles)

www.baeldung.com/spring-prof…

maven.apache.org/guides/intr…