Spring Boot以编程方式设置配置文件 [英] Spring Boot Programmatically setting profiles

查看:286
本文介绍了Spring Boot以编程方式设置配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Spring Boot Application中设置活动配置文件.该应用程序将部署在独立的Tomcat中.

How to set active profile in spring boot Application. This application will be deployed in stand alone Tomcat.

我有2个属性文件application- {profile} .properties.

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

我的应用程序类

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

如果我运行带有嵌入式tomcat的应用程序,则将开发配置文件设置为活动状态,并且工作正常.但是当我在单独的tomcat中部署时.它不起作用.

if I run the app with embedded tomcat the dev profile is set as active and it works fine. But when I deploy in stand alone tomcat. It does not work.

我试图在configure方法中设置活动配置文件.但是当我从上下文中获取环境时,我得到了空指针异常.

I tried to set active profile in configure method. but i get null pointer exception, when i get the environment from the context.

有关如何设置活动配置文件的任何帮助.

Any help on how to set the active profile.

推荐答案

我也遇到了同样的问题,在奋斗了半天后,我最终遇到了这个问题:

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

这篇关于Spring Boot以编程方式设置配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆