Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 717
Michał Margiel Avatar asked Apr 06 '12 09:04

Michał Margiel


People also ask

What is spring boot default profile?

The default profile is always active. Spring Boot loads all properties in application. yml into the default profile. We could rename the configuration file to application-default.

How can I see my spring boot profile?

In this example, We have determined active profile in spring boot. * Created by JavaDeveloperZone on 28-04-2018. String[] activeProfiles = environment. getActiveProfiles(); // it will return String Array of all active profile.

How many profile can be defined for a spring application?

In the application. properties we have set the local profile to be active. With the SpringApplicationBuilder's profiles method we add two additional profiles.

Are Spring profile names case sensitive?

Spring profiles are enabled using the case insensitive tokens spring. profiles. active or spring_profiles_active .


2 Answers

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 
like image 122
andih Avatar answered Sep 30 '22 13:09

andih


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.

like image 20
Paul Philion Avatar answered Sep 30 '22 13:09

Paul Philion