Default profile in Spring 3.1

前端 未结 7 1017
执笔经年
执笔经年 2020-11-28 19:56

In my application I have beans annotated with @Profile(\"prod\") and @Profile(\"demo\"). The first one, as you can guess :), is used on beans that

相关标签:
7条回答
  • 2020-11-28 20:21

    About setting default production profile already posted @andih

    The easiest way to set default profile for maven jetty plugin, is to include below element in your plugin configuration:

    <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <configuration>
            <systemProperties>
                <systemProperty>
                    <name>spring.profiles.active</name>
                    <value>demo</value>
                </systemProperty>
            </systemProperties>
        </configuration>
    </plugin>
    
    0 讨论(0)
  • 2020-11-28 20:22

    You may also consider removing the PROD profile, and use @Profile("!demo")

    0 讨论(0)
  • 2020-11-28 20:30

    Define your production environment as default profile in your web.xml

    <context-param>
       <param-name>spring.profiles.default</param-name>
       <param-value>prod</param-value>
    </context-param>
    

    and if you want to use a different profile pass it as system property

    mvn -Dspring.profiles.active="demo" jetty:run
    
    0 讨论(0)
  • 2020-11-28 20:34

    Spring provide two separate properties when determining which profiles are active:

    • spring.profiles.active

    and

    • spring.profiles.default

    If spring.profiles.active is set, then its value determines which profiles are active. But if spring.profiles.active isn't set, then Spring looks to spring.profiles.default.

    If neither spring.profiles.active nor spring.profiles.default is set, then there are no active profiles, and only those beans that aren't defined as being in a profile are created.Any bean that does not specify a profile belongs to default profile.

    0 讨论(0)
  • 2020-11-28 20:36

    My experience is that using

    @Profile("default")
    

    the bean will only be added to the context if no other profile is identified. If you pass in a different profile, e.g. -Dspring.profiles.active="demo", this profile is ignored.

    0 讨论(0)
  • 2020-11-28 20:40

    I have the same issue, but I use WebApplicationInitializer in order to configure the ServletContext programmatically (Servlet 3.0+). So I do the following:

    public class WebAppInitializer implements WebApplicationInitializer {
    
        @Override
        public void onStartup(ServletContext sc) throws ServletException {
            // Create the 'root' Spring application context
            final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
            // Default active profiles can be overridden by the environment variable 'SPRING_PROFILES_ACTIVE'
            rootContext.getEnvironment().setDefaultProfiles("prod");
            rootContext.register(AppConfig.class);
    
            // Manage the lifecycle of the root application context
            sc.addListener(new ContextLoaderListener(rootContext));
        }
    }
    
    0 讨论(0)
提交回复
热议问题