• How to Set Profiles for Spring


    Activate and set the profiles so that the respective beans are registered in the container.

    1. Programmatically via WebApplicationInitializer Interface

    # In web applications
    @Configuration
    public class MyWebApplicationInitializer implements WebApplicationInitializer {
    
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
     
            servletContext.setInitParameter("spring.profiles.active", "dev");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2. Programmatically via ConfigurableEnvironment

    @Autowired
    private ConfigurableEnvironment env;
    ...
    env.setActiveProfiles("dev");
    
    • 1
    • 2
    • 3
    • 4

    3. Context Parameter in web.xml

    # in the web.xml
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/app-config.xml</param-value>
    </context-param>
    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>dev</param-value>
    </context-param>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4. JVM System Parameter

     -Dspring.profiles.active=dev
    
    • 1

    5. Environment Variable

    # In a Unix environment, profiles can also be activated via the environment variable
    export spring_profiles_active=dev
    
    • 1
    • 2

    6. Maven Profile

    # In every Maven profile, we can set a spring.profiles.active property
    <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>
    
    # Its value will be used to replace the @spring.profiles.active@ placeholder in application.properties:
    spring.profiles.active=@spring.profiles.active@
    
    # enable resource filtering in pom.xml
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        ...
    </build>
    
    # append a -P parameter to switch which Maven profile will be applied
    mvn clean package -Pprod
    
    • 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

    7. @ActiveProfile in Tests

    @ActiveProfiles("dev")
    
    • 1

    Activating profiles priority rom highest to lowest priority
    1、Context parameter in web.xml
    2、WebApplicationInitializer
    3、JVM System parameter
    4、Environment variable
    5、Maven profile

    参考:Spring Profiles

  • 相关阅读:
    车载通信架构 —— SOME/IP-SD 协议介绍
    L2-048 寻宝图(PTA)
    linux安装Jdk
    如何在 JavaScript 中使用三元运算符
    Docker - WEB应用实例
    单片非晶磁性测量系统典型磁参数的不确定度与重复性
    排序算法优化(二)
    pycharm找不到sklearn包
    【Linux】 - Linux中的重定向和管道符
    Docker基础-namespace
  • 原文地址:https://blog.csdn.net/weixin_37646636/article/details/133777723