16

tl;dr: how to create a Spring context based on an annotation-based configuration class, while supplying active profiles?

I am trying to create a Spring context using a class with the configuration specified using annotations.

org.springframework.context.ApplicationContext context = 
    new org.springframework.context.annotation.AnnotationConfigApplicationContext( com.initech.ReleaserConfig.class );

However when it starts up it crashes because it cannot find a required bean, but that bean does exist : albeit only under a certain profile "myProfile".

How do I specify the active profile? I have a feeling I need to use one the org.springframework.context.annotation.AnnotationConfigApplicationContext(org.springframework.beans.factory.support.DefaultListableBeanFactory) constructor but I'm not sure which ones is appropriate.


PS- I am not using Spring Boot, but am on Spring 4.2.4.RELEASE.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Sled
  • 18,541
  • 27
  • 119
  • 168

2 Answers2

17

This should do the trick...

final AnnotationConfigApplicationContext appContext =  new AnnotationConfigApplicationContext();
appContext.getEnvironment().setActiveProfiles( "myProfile" );
appContext.register( com.initech.ReleaserConfig.class );
appContext.refresh();
Sled
  • 18,541
  • 27
  • 119
  • 168
Jamie Bisotti
  • 2,605
  • 19
  • 23
  • That won't work since instantiating the application context causes the exception so I can never get to the `.getEnvironment()` call. – Sled Oct 26 '16 at 19:00
  • your answer led to the solution so I completed it so that it works – Sled Oct 26 '16 at 19:10
  • 2
    @ArtB you can use `System.setProperty("spring.profiles.active", "myProfile");` before instantiate the context – xedo Jun 26 '17 at 18:02
  • Hi @ArtB How you solved this problem ? can you please share more information ? I am also stuck at same problem find my question here https://stackoverflow.com/questions/46422271/how-to-dynamically-switch-application-context-in-spring-security – Amit Sep 29 '17 at 12:09
  • The accepted answer solved my problem and I even editted to include the missing details I needed. – Sled Sep 29 '17 at 19:26
  • Or you can set Windows System Environment Variable as `SPRING_PROFILES_ACTIVE` = `myProfile`. (You need to restart your IDE if you run the code via IDE.) – wonsuc Jan 03 '23 at 08:25
  • Or you can add Environment Variable as `SPRING_PROFILES_ACTIVE` = `myProfile` from your IDE's run configuration for test. – wonsuc Jan 03 '23 at 08:33
  • These last two comments describe non-programmatic ways of setting the active Spring profiles. Both are fine options; however, this question asked for a programmatic way to do it. – Jamie Bisotti Feb 02 '23 at 17:01
2

If you want classpath:resources/application-${spring.profiles.active}.yml to be working, setting system properties before refreshing is the way to go.

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.getEnvironment().getSystemProperties().put(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
applicationContext.scan("com.sample");
applicationContext.refresh();
RajVimalC
  • 657
  • 6
  • 7