Close

Spring Framework - Environment Examples

Spring Framework 

Setting active profile for this Environment. Environment#setActiveProfiles will replace existing active profiles. Use Environment#addActiveProfile() to add a profile while preserving the existing profiles.

        AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
context.getEnvironment()
.setActiveProfiles(PROFILE_SWING, DataConfig.PROFILE_LOCAL);
context.register(AppConfig.class);
context.refresh();
Original Post




@Configuration
@PropertySource("classpath:app.properties")
public class PropertySourceExample {

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(PropertySourceExample.class);

ConfigurableEnvironment env = context.getEnvironment();
String property = env.getProperty("some-strProp");
System.out.println("some-strProp value is " + property);
//printing all sources
System.out.println(env.getPropertySources());
}
}
Original Post




See Also