• 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

  • 相关阅读:
    《论文阅读》同情对话生成的知识桥梁 AAAI 2021
    windows查看后台执行中的python或bat脚本
    2022最新PPT模板,免费下载
    element ui 的tree单选,控制setCheckedNodes传入data即可
    go使用zap + lumberjack重构项目的日志系统
    2243. 计算字符串的数字和
    5、Docker安装mysql主从复制与redis集群
    前端面试-html、css
    蓝桥等考Python组别八级003
    [ROC-RK3568-PC] [Firefly-Android] 10min带你了解PWM的使用
  • 原文地址:https://blog.csdn.net/weixin_37646636/article/details/133777723