-1

Can we declare a special spring bean which can be instantiated before spring checks for the Spring Profiles declarations? The challenge is to set the active spring profile from a spring bean , but it seems spring looks for declared profiles and then goes for bean instantiation.

I have this weird requirement because I need to set the active profile but cannot use -Dspring.profiles.active=profilename. So, I need to do it in code by using System.setProperty(). Also, I cannot use any SpringContextListener because it is not a web application.

Prakash
  • 461
  • 1
  • 5
  • 21

1 Answers1

-1

If you are using Spring in a command-line program, you likely have a main class that looks something like this:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).run(args);
    }
}

You can specify active profiles by calling the profiles() method:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class)
                .profiles("myprofile", "otherprofile")
                .run(args);
    }
}

If you are using Spring in a Servlet Container, you likely have a Servlet Initializer that looks something like this:

public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

You can specify active profiles by calling the profiles() method:

public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class)
                          .profiles("myprofile", "otherprofile");
    }
}

You can of course write code to dynamically determine which profiles to activate.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • In my case the spring context is created by Mule ESB. The profiles (and other beans) are declared in xml. It seems Mule combines the various xml files and then creates the context and it first determines the active profile and applies it. I've tried to create beans to set the system property to activate but it does not work as this bean is created after the profile is activated. – Prakash Feb 19 '18 at 09:44
  • @Prakash So what you're really asking is how to configure Mule ESB to set active profiles. Maybe you should have mentioned that in your question, and you'd have gotten answer tht is more on point, and I wouldn't have wasted time writing this answer. – Andreas Feb 19 '18 at 15:07
  • I never said it was spring boot application. so why are you using SpringBootApplication in your example. – Prakash Feb 19 '18 at 17:17
  • The problem is not specific to mule. The situation i have described could happen in any scenario where you have custom third party class creating the spring context. – Prakash Feb 19 '18 at 17:26