Michał Margiel
Michał Margiel

Reputation: 3002

Default profile in Spring 3.1

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 connect to production DB and second one annotates beans that use some fake DB (HashMap or whatever)- to make development faster.

What I would like to have is default profile ("prod") that will be used always if it is not overridden by "something-else".

Perfect would be to have in my web.xml:

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

and then override this with -Dspring.profiles.active="demo" so that I could do:

mvn jetty:run -Dspring.profiles.active="demo". 

But sadly this is not working. Any idea how could I achive that? Setting -Dspring.profiles.active="prod" on all my environments is not an option.

Upvotes: 100

Views: 71567

Answers (7)

Touhidur Rahaman Khan
Touhidur Rahaman Khan

Reputation: 332

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.

Upvotes: 4

mcoolive
mcoolive

Reputation: 4205

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));
    }
}

Upvotes: 6

blacelle
blacelle

Reputation: 2217

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

Upvotes: 5

andih
andih

Reputation: 5603

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

Upvotes: 111

Jakub Kubrynski
Jakub Kubrynski

Reputation: 14149

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>

Upvotes: 3

Paul Philion
Paul Philion

Reputation: 706

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.

Upvotes: 69

Hurda
Hurda

Reputation: 4715

You can setup your web.xml as filtered resource and have this value filled by maven from maven profile settings - that what we do.

in pom filter all resources (you can do taht if you have no ${} marking in them)

<webResources>
    <resource>
        <directory>src/main/webapp</directory>
        <filtering>true</filtering>
    </resource>
</webResources>

in web.xml put

<context-param>
     <param-name>spring.profiles.active</param-name>
     <param-value>${spring.prfile}</param-value>
</context-param>

in pom create maven profiles

<profiles>
    <profile>
        <id>DEFAULT</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <spring.profile>prod</spring.profile>
        </properties>
    <profile>
    <profile>
        <id>DEMO</id>
        <properties>
            <spring.profile>demo</spring.profile>
        </properties>
    <profile>
</profiles>

Now you can use

mvn jetty:run -P DEMO

or simply -P DEMO with any maven command

Upvotes: -1

Related Questions