• 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

  • 相关阅读:
    iOS 17.4报错: libopencore-amrnb.a[arm64]
    论文阅读_扩散模型_DDPM
    Linux定时任务配置
    凤凰价格——架构师的视角
    Docker02基本管理
    Redis维护缓存的方案选择
    ansible的个人笔记使用记录-个人心得总结
    国内各大安卓应用市场的不同ASO优化点
    java生产者 消费者模式概念讲解
    2022谷粒商城学习笔记(一)环境配置
  • 原文地址:https://blog.csdn.net/weixin_37646636/article/details/133777723