Spring Download Software Defined Profiles

How to set active profile in spring boot application. This application will be deployed in standalone Tomcat.

I have 2 application- {profile} .properties properties files.

My application class

@SpringBootApplication public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) { System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev"); ApplicationContext ctx = SpringApplication.run(Application.class, args); } } 

if I run the application with built-in tomcat, the dev profile is set as active and it works fine. But when I turn around in a solitary cat. This does not work.

I tried to set the active profile in the configure method. but I get a null pointer exception when I get the environment out of context.

Any help on setting up an active profile.

+6
source share
3 answers

I also had the same problem, and after the battle in half a day, I ended up with this:

  @SpringBootApplication public class MyApplication extends SpringBootServletInitializer { public static void main(String[] args) { System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev"); SpringApplication.run(MyApplication.class, args); } @Override public void onStartup(ServletContext servletContext) throws ServletException { System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev"); super.onStartup(servletContext); } } 
+6
source

Instead of dynamically activating the profile, you can put the profiles as vm arguments in the catalina.sh file

 CATALINA_OPTS="-Dspring.profiles.active=dev" 
+1
source

System.setProperty ("spring.profiles.active", "dev");

0
source

All Articles