Java Code Examples for org.springframework.context.annotation.AnnotationConfigApplicationContext#setEnvironment()

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#setEnvironment() . These examples are extracted from open source projects. 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
@Test
public void beanConditionOn() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.setEnvironment(new MockEnvironment().withProperty("bar.enabled", "true"));
	ctx.register(BeanConditionConfig.class);
	ctx.refresh();
	this.context = ctx;

	FooService service = this.context.getBean(FooService.class);
	Cache cache = getCache();

	Object key = new Object();
	Object value = service.getWithCondition(key);
	assertCacheHit(key, value, cache);
	value = service.getWithCondition(key);
	assertCacheHit(key, value, cache);

	assertEquals(2, this.context.getBean(BeanConditionConfig.Bar.class).count);
}
 
Example 2
Source Project: syndesis   File: SyndesisCommand.java    License: Apache License 2.0 6 votes vote down vote up
private AbstractApplicationContext createContext() {
    final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

    final YamlPropertySourceLoader propertySourceLoader = new YamlPropertySourceLoader();
    final List<PropertySource<?>> yamlPropertySources;
    try {
        yamlPropertySources = propertySourceLoader.load(name, context.getResource("classpath:" + name + ".yml"));
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }

    final StandardEnvironment environment = new StandardEnvironment();
    final MutablePropertySources propertySources = environment.getPropertySources();
    propertySources.addFirst(new MapPropertySource("parameters", parameters));
    yamlPropertySources.forEach(propertySources::addLast);

    context.setEnvironment(environment);

    final String packageName = getClass().getPackage().getName();
    context.scan(packageName);

    context.refresh();

    return context;
}
 
Example 3
AbstractApplicationContext createApplicationContext(Domain domain) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(gatewayApplicationContext);
    context.setClassLoader(new ReactorHandlerClassLoader(gatewayApplicationContext.getClassLoader()));
    context.setEnvironment((ConfigurableEnvironment) gatewayApplicationContext.getEnvironment());

    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setIgnoreUnresolvablePlaceholders(true);
    configurer.setEnvironment(gatewayApplicationContext.getEnvironment());
    context.addBeanFactoryPostProcessor(configurer);

    context.getBeanFactory().registerSingleton("domain", domain);
    context.register(HandlerConfiguration.class);
    context.setId("context-domain-" + domain.getId());
    context.refresh();

    return context;
}
 
Example 4
@Test
public void beanConditionOn() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.setEnvironment(new MockEnvironment().withProperty("bar.enabled", "true"));
	ctx.register(BeanConditionConfig.class);
	ctx.refresh();
	this.context = ctx;

	FooService service = this.context.getBean(FooService.class);
	Cache cache = getCache();

	Object key = new Object();
	Object value = service.getWithCondition(key);
	assertCacheHit(key, value, cache);
	value = service.getWithCondition(key);
	assertCacheHit(key, value, cache);

	assertEquals(2, this.context.getBean(BeanConditionConfig.Bar.class).count);
}
 
Example 5
@Test
public void testPlaceholderBased() throws Exception {
	MockEnvironment env = new MockEnvironment();
	env.setProperty("serverName", "server");
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.setEnvironment(env);
	ctx.register(PlaceholderBasedConfiguration.class);
	ctx.refresh();
	try {
		MBeanServer server = (MBeanServer) ctx.getBean("server");
		ObjectName oname = ObjectNameManager.getInstance("bean:name=testBean4");
		assertNotNull(server.getObjectInstance(oname));
		String name = (String) server.getAttribute(oname, "Name");
		assertEquals("Invalid name returned", "TEST", name);
	}
	finally {
		ctx.close();
	}
}
 
Example 6
@Test
public void testPlaceholderBased() throws Exception {
	MockEnvironment env = new MockEnvironment();
	env.setProperty("serverName", "server");
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.setEnvironment(env);
	context.register(PlaceholderBasedConfiguration.class);
	context.refresh();
	this.ctx = context;
	validateAnnotationTestBean();
}
 
Example 7
@Test
public void annotationConfigApplicationContext_withPojos() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

	assertHasStandardEnvironment(ctx);
	ctx.setEnvironment(prodEnv);

	ctx.register(EnvironmentAwareBean.class);
	ctx.refresh();

	assertEnvironmentAwareInvoked(ctx, prodEnv);
}
 
Example 8
@Test
public void mostSpecificDerivedClassDrivesEnvironment_withDevEnvAndDerivedDevConfigClass() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.setEnvironment(devEnv);
	ctx.register(DerivedDevConfig.class);
	ctx.refresh();

	assertThat("should not have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(false));
	assertThat("should not have derived dev bean", ctx.containsBean(DERIVED_DEV_BEAN_NAME), is(false));
	assertThat("should not have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(false));
}
 
Example 9
@Test
public void annotationConfigApplicationContext_withProdEnvAndDevConfigClass() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

	assertHasStandardEnvironment(ctx);
	ctx.setEnvironment(prodEnv);

	ctx.register(DevConfig.class);
	ctx.refresh();

	assertThat("should not have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(false));
	assertThat("should not have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(false));
}
 
Example 10
@Test
public void annotationConfigApplicationContext_withImportedConfigClasses() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

	assertHasStandardEnvironment(ctx);
	ctx.setEnvironment(prodEnv);

	ctx.register(Config.class);
	ctx.refresh();

	assertEnvironmentAwareInvoked(ctx, prodEnv);
	assertThat("should have prod bean", ctx.containsBean(PROD_BEAN_NAME), is(true));
	assertThat("should not have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(false));
	assertThat("should not have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(false));
}
 
Example 11
@Test
public void mostSpecificDerivedClassDrivesEnvironment_withDevEnvAndDerivedDevConfigClass() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.setEnvironment(devEnv);
	ctx.register(DerivedDevConfig.class);
	ctx.refresh();

	assertThat("should not have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(false));
	assertThat("should not have derived dev bean", ctx.containsBean(DERIVED_DEV_BEAN_NAME), is(false));
	assertThat("should not have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(false));
}
 
Example 12
private void testProfileExpression(boolean expected, String... activeProfiles) {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	StandardEnvironment environment = new StandardEnvironment();
	environment.setActiveProfiles(activeProfiles);
	ctx.setEnvironment(environment);
	ctx.register(ProfileExpressionConfig.class);
	ctx.refresh();
	assertThat("wrong presence of expression bean",
			ctx.containsBean("expressionBean"), is(expected));
}
 
Example 13
@Test
public void annotationConfigApplicationContext_withProdEnvAndDevConfigClass() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

	assertHasStandardEnvironment(ctx);
	ctx.setEnvironment(prodEnv);

	ctx.register(DevConfig.class);
	ctx.refresh();

	assertThat("should not have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(false));
	assertThat("should not have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(false));
}
 
Example 14
@Test
public void annotationConfigApplicationContext_withProdEnvAndProdConfigClass() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

	assertHasStandardEnvironment(ctx);
	ctx.setEnvironment(prodEnv);

	ctx.register(ProdConfig.class);
	ctx.refresh();

	assertThat("should have prod bean", ctx.containsBean(PROD_BEAN_NAME), is(true));
}
 
Example 15
@Test
public void annotationConfigApplicationContext_withDevEnvAndDevConfigClass() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

	assertHasStandardEnvironment(ctx);
	ctx.setEnvironment(devEnv);

	ctx.register(DevConfig.class);
	ctx.refresh();

	assertThat("should have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(true));
	assertThat("should have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(true));
}
 
Example 16
@Test
public void annotationConfigApplicationContext_withPojos() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

	assertHasStandardEnvironment(ctx);
	ctx.setEnvironment(prodEnv);

	ctx.register(EnvironmentAwareBean.class);
	ctx.refresh();

	assertEnvironmentAwareInvoked(ctx, prodEnv);
}
 
Example 17
@Test
public void annotationConfigApplicationContext_withDevEnvAndDevConfigClass() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

	assertHasStandardEnvironment(ctx);
	ctx.setEnvironment(devEnv);

	ctx.register(DevConfig.class);
	ctx.refresh();

	assertThat("should have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(true));
	assertThat("should have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(true));
}
 
Example 18
@Test
public void annotationConfigApplicationContext_withImportedConfigClasses() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

	assertHasStandardEnvironment(ctx);
	ctx.setEnvironment(prodEnv);

	ctx.register(Config.class);
	ctx.refresh();

	assertEnvironmentAwareInvoked(ctx, prodEnv);
	assertThat("should have prod bean", ctx.containsBean(PROD_BEAN_NAME), is(true));
	assertThat("should not have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(false));
	assertThat("should not have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(false));
}
 
Example 19
@Test
public void mostSpecificDerivedClassDrivesEnvironment_withDerivedDevEnvAndDerivedDevConfigClass() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	StandardEnvironment derivedDevEnv = new StandardEnvironment();
	derivedDevEnv.setActiveProfiles(DERIVED_DEV_ENV_NAME);
	ctx.setEnvironment(derivedDevEnv);
	ctx.register(DerivedDevConfig.class);
	ctx.refresh();

	assertThat("should have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(true));
	assertThat("should have derived dev bean", ctx.containsBean(DERIVED_DEV_BEAN_NAME), is(true));
	assertThat("should have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(true));
}
 
Example 20
private void testProfileExpression(boolean expected, String... activeProfiles) {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	StandardEnvironment environment = new StandardEnvironment();
	environment.setActiveProfiles(activeProfiles);
	ctx.setEnvironment(environment);
	ctx.register(ProfileExpressionConfig.class);
	ctx.refresh();
	assertThat("wrong presence of expression bean",
			ctx.containsBean("expressionBean"), is(expected));
}