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

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#setEnvironment() . 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: EnableCachingIntegrationTests.java    From spring-analysis-note with MIT License 7 votes vote down vote up
@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 File: EnableMBeanExportConfigurationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: SyndesisCommand.java    From syndesis with 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 4
Source File: SecurityDomainRouterFactory.java    From graviteeio-access-management with Apache License 2.0 6 votes vote down vote up
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 5
Source File: EnableCachingIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 6
Source File: EnvironmentSystemIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 7
Source File: EnableMBeanExportConfigurationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 8
Source File: EnvironmentSystemIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 9
Source File: EnvironmentSystemIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 10
Source File: EnvironmentSystemIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 11
Source File: EnvironmentSystemIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void annotationConfigApplicationContext_withPojos() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

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

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

	assertEnvironmentAwareInvoked(ctx, prodEnv);
}
 
Example 12
Source File: EnvironmentSystemIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 13
Source File: EnvironmentSystemIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 14
Source File: EnvironmentSystemIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 15
Source File: EnvironmentSystemIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 16
Source File: EnvironmentSystemIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 17
Source File: EnvironmentSystemIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 18
Source File: EnvironmentSystemIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 19
Source File: EnvironmentSystemIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 20
Source File: EnvironmentSystemIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void annotationConfigApplicationContext_withPojos() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

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

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

	assertEnvironmentAwareInvoked(ctx, prodEnv);
}