Java Code Examples for org.springframework.core.env.StandardEnvironment#setActiveProfiles()

The following examples show how to use org.springframework.core.env.StandardEnvironment#setActiveProfiles() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScmContextRefresher.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
/**
 * Don't use ConfigurableEnvironment.merge() in case there are clashes with
 * property source names
 * 
 * @param input
 * @return
 */
private StandardEnvironment copyEnvironment(ConfigurableEnvironment input) {
	StandardEnvironment environment = new StandardEnvironment();
	MutablePropertySources capturedPropertySources = environment.getPropertySources();

	// Only copy the default property source(s) and the profiles over from
	// the main environment (everything else should be pristine, just like
	// it was on startup).
	for (String name : DEFAULT_PROPERTY_SOURCES) {
		if (input.getPropertySources().contains(name)) {
			if (capturedPropertySources.contains(name)) {
				capturedPropertySources.replace(name, input.getPropertySources().get(name));
			} else {
				capturedPropertySources.addLast(input.getPropertySources().get(name));
			}
		}
	}
	environment.setActiveProfiles(input.getActiveProfiles());
	environment.setDefaultProfiles(input.getDefaultProfiles());

	Map<String, Object> map = new HashMap<String, Object>();
	map.put("spring.jmx.enabled", false);
	map.put("spring.main.sources", "");
	capturedPropertySources.addFirst(new MapPropertySource(SCM_REFRESH_PROPERTY_SOURCE, map));
	return environment;
}
 
Example 2
Source File: ProfileXmlBeanDefinitionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private BeanDefinitionRegistry beanFactoryFor(String xmlName, String... activeProfiles) {
	DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
	StandardEnvironment env = new StandardEnvironment();
	env.setActiveProfiles(activeProfiles);
	reader.setEnvironment(env);
	reader.loadBeanDefinitions(new ClassPathResource(xmlName, getClass()));
	return beanFactory;
}
 
Example 3
Source File: ProfileXmlBeanDefinitionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private BeanDefinitionRegistry beanFactoryFor(String xmlName, String... activeProfiles) {
	DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
	StandardEnvironment env = new StandardEnvironment();
	env.setActiveProfiles(activeProfiles);
	reader.setEnvironment(env);
	reader.loadBeanDefinitions(new ClassPathResource(xmlName, getClass()));
	return beanFactory;
}
 
Example 4
Source File: ProfileXmlBeanDefinitionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private BeanDefinitionRegistry beanFactoryFor(String xmlName, String... activeProfiles) {
	DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
	StandardEnvironment env = new StandardEnvironment();
	env.setActiveProfiles(activeProfiles);
	reader.setEnvironment(env);
	reader.loadBeanDefinitions(new ClassPathResource(xmlName, getClass()));
	return beanFactory;
}
 
Example 5
Source File: ContextRefresher.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
private StandardEnvironment copyEnvironment(ConfigurableEnvironment input) {
	StandardEnvironment environment = new StandardEnvironment();
	MutablePropertySources capturedPropertySources = environment.getPropertySources();
	// Only copy the default property source(s) and the profiles over from the main
	// environment (everything else should be pristine, just like it was on startup).
	for (String name : DEFAULT_PROPERTY_SOURCES) {
		if (input.getPropertySources().contains(name)) {
			if (capturedPropertySources.contains(name)) {
				capturedPropertySources.replace(name,
						input.getPropertySources().get(name));
			}
			else {
				capturedPropertySources.addLast(input.getPropertySources().get(name));
			}
		}
	}
	environment.setActiveProfiles(input.getActiveProfiles());
	environment.setDefaultProfiles(input.getDefaultProfiles());
	Map<String, Object> map = new HashMap<String, Object>();
	map.put("spring.jmx.enabled", false);
	map.put("spring.main.sources", "");
	// gh-678 without this apps with this property set to REACTIVE or SERVLET fail
	map.put("spring.main.web-application-type", "NONE");
	capturedPropertySources
			.addFirst(new MapPropertySource(REFRESH_ARGS_PROPERTY_SOURCE, map));
	return environment;
}
 
Example 6
Source File: ConfigMapPropertySource.java    From spring-cloud-kubernetes with Apache License 2.0 4 votes vote down vote up
private static Environment createEnvironmentWithActiveProfiles(
		String[] activeProfiles) {
	StandardEnvironment environment = new StandardEnvironment();
	environment.setActiveProfiles(activeProfiles);
	return environment;
}