Spring Boot Programmatically setting profiles

核能气质少年 提交于 2019-11-28 23:46:31

I also had the same problem and after struggling for 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);
        }
    }

You can set additional profiles on start up:

   SpringApplication springApp = new SpringApplication(Main.class);
   springApplication.setAdditionalProfiles("profile1", "profile2");
   springApplication.run(args);

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

CATALINA_OPTS="-Dspring.profiles.active=dev"

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!