org.springframework.context.annotation.AnnotationConfigApplicationContext#setEnvironment ( )源码实例Demo

下面列出了org.springframework.context.annotation.AnnotationConfigApplicationContext#setEnvironment ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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);
}
 
源代码2 项目: syndesis   文件: SyndesisCommand.java
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;
}
 
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;
}
 
@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);
}
 
@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();
	}
}
 
@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();
}
 
@Test
public void annotationConfigApplicationContext_withPojos() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

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

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

	assertEnvironmentAwareInvoked(ctx, prodEnv);
}
 
@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));
}
 
@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));
}
 
@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));
}
 
@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));
}
 
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));
}
 
@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));
}
 
@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));
}
 
@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));
}
 
@Test
public void annotationConfigApplicationContext_withPojos() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

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

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

	assertEnvironmentAwareInvoked(ctx, prodEnv);
}
 
@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));
}
 
@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));
}
 
@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));
}
 
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));
}