Java Code Examples for org.springframework.context.ConfigurableApplicationContext#getParent()

The following examples show how to use org.springframework.context.ConfigurableApplicationContext#getParent() . 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: ContextHierarchyDirtiesContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void runTestAndVerifyHierarchies(Class<? extends FooTestCase> testClass, boolean isFooContextActive,
		boolean isBarContextActive, boolean isBazContextActive) {

	JUnitCore jUnitCore = new JUnitCore();
	Result result = jUnitCore.run(testClass);
	assertTrue("all tests passed", result.wasSuccessful());

	assertThat(ContextHierarchyDirtiesContextTests.context, notNullValue());

	ConfigurableApplicationContext bazContext = (ConfigurableApplicationContext) ContextHierarchyDirtiesContextTests.context;
	assertEquals("baz", ContextHierarchyDirtiesContextTests.baz);
	assertThat("bazContext#isActive()", bazContext.isActive(), is(isBazContextActive));

	ConfigurableApplicationContext barContext = (ConfigurableApplicationContext) bazContext.getParent();
	assertThat(barContext, notNullValue());
	assertEquals("bar", ContextHierarchyDirtiesContextTests.bar);
	assertThat("barContext#isActive()", barContext.isActive(), is(isBarContextActive));

	ConfigurableApplicationContext fooContext = (ConfigurableApplicationContext) barContext.getParent();
	assertThat(fooContext, notNullValue());
	assertEquals("foo", ContextHierarchyDirtiesContextTests.foo);
	assertThat("fooContext#isActive()", fooContext.isActive(), is(isFooContextActive));
}
 
Example 2
Source File: ContextHierarchyDirtiesContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void runTestAndVerifyHierarchies(Class<? extends FooTestCase> testClass, boolean isFooContextActive,
		boolean isBarContextActive, boolean isBazContextActive) {

	JUnitCore jUnitCore = new JUnitCore();
	Result result = jUnitCore.run(testClass);
	assertTrue("all tests passed", result.wasSuccessful());

	assertThat(ContextHierarchyDirtiesContextTests.context, notNullValue());

	ConfigurableApplicationContext bazContext = (ConfigurableApplicationContext) ContextHierarchyDirtiesContextTests.context;
	assertEquals("baz", ContextHierarchyDirtiesContextTests.baz);
	assertThat("bazContext#isActive()", bazContext.isActive(), is(isBazContextActive));

	ConfigurableApplicationContext barContext = (ConfigurableApplicationContext) bazContext.getParent();
	assertThat(barContext, notNullValue());
	assertEquals("bar", ContextHierarchyDirtiesContextTests.bar);
	assertThat("barContext#isActive()", barContext.isActive(), is(isBarContextActive));

	ConfigurableApplicationContext fooContext = (ConfigurableApplicationContext) barContext.getParent();
	assertThat(fooContext, notNullValue());
	assertEquals("foo", ContextHierarchyDirtiesContextTests.foo);
	assertThat("fooContext#isActive()", fooContext.isActive(), is(isFooContextActive));
}
 
Example 3
Source File: ContextHierarchyDirtiesContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private void runTestAndVerifyHierarchies(Class<? extends FooTestCase> testClass, boolean isFooContextActive,
		boolean isBarContextActive, boolean isBazContextActive) {

	JUnitCore jUnitCore = new JUnitCore();
	Result result = jUnitCore.run(testClass);
	assertTrue("all tests passed", result.wasSuccessful());

	assertThat(ContextHierarchyDirtiesContextTests.context, notNullValue());

	ConfigurableApplicationContext bazContext = (ConfigurableApplicationContext) ContextHierarchyDirtiesContextTests.context;
	assertEquals("baz", ContextHierarchyDirtiesContextTests.baz);
	assertThat("bazContext#isActive()", bazContext.isActive(), is(isBazContextActive));

	ConfigurableApplicationContext barContext = (ConfigurableApplicationContext) bazContext.getParent();
	assertThat(barContext, notNullValue());
	assertEquals("bar", ContextHierarchyDirtiesContextTests.bar);
	assertThat("barContext#isActive()", barContext.isActive(), is(isBarContextActive));

	ConfigurableApplicationContext fooContext = (ConfigurableApplicationContext) barContext.getParent();
	assertThat(fooContext, notNullValue());
	assertEquals("foo", ContextHierarchyDirtiesContextTests.foo);
	assertThat("fooContext#isActive()", fooContext.isActive(), is(isFooContextActive));
}
 
Example 4
Source File: ContextRefresherTests.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void parentContextIsClosed() {
	// Use spring.cloud.bootstrap.name to switch off the defaults (which would pick up
	// a bootstrapProperties immediately
	try (ConfigurableApplicationContext context = SpringApplication.run(
			ContextRefresherTests.class, "--spring.main.web-application-type=none",
			"--debug=false", "--spring.main.bannerMode=OFF",
			"--spring.cloud.bootstrap.name=refresh")) {
		ContextRefresher refresher = new ContextRefresher(context, this.scope);
		TestPropertyValues.of("spring.cloud.bootstrap.sources: "
				+ "org.springframework.cloud.context.refresh.ContextRefresherTests.PropertySourceConfiguration")
				.applyTo(context);
		ConfigurableApplicationContext refresherContext = refresher
				.addConfigFilesToEnvironment();
		then(refresherContext.getParent()).isNotNull()
				.isInstanceOf(ConfigurableApplicationContext.class);
		ConfigurableApplicationContext parent = (ConfigurableApplicationContext) refresherContext
				.getParent();
		then(parent.isActive()).isFalse();
	}
}
 
Example 5
Source File: BootstrapListenerHierarchyIntegrationTests.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddInOneBootstrapForABasicParentChildHierarchy() {
	ConfigurableApplicationContext context = new SpringApplicationBuilder()
			.sources(RootConfiguration.class).web(NONE)
			.child(BasicConfiguration.class).web(NONE).run();

	// Should be RootConfiguration based context
	ConfigurableApplicationContext parent = (ConfigurableApplicationContext) context
			.getParent();
	then(parent.getBean("rootBean", String.class)).isEqualTo("rootBean");

	// Parent should have the bootstrap context as parent
	then(parent.getParent()).isNotNull();

	ConfigurableApplicationContext bootstrapContext = (ConfigurableApplicationContext) parent
			.getParent();

	// Bootstrap should be the root, there should be no other parent
	then(bootstrapContext.getParent()).isNull();
}
 
Example 6
Source File: BootstrapListenerHierarchyIntegrationTests.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddInOneBootstrapForSiblingsBasedHierarchy() {
	ConfigurableApplicationContext context = new SpringApplicationBuilder()
			.sources(RootConfiguration.class).web(NONE)
			.child(BasicConfiguration.class).web(NONE)
			.sibling(BasicConfiguration.class).web(NONE).run();

	// Should be RootConfiguration based context
	ConfigurableApplicationContext parent = (ConfigurableApplicationContext) context
			.getParent();
	then(parent.getBean("rootBean", String.class)).isEqualTo("rootBean");

	// Parent should have the bootstrap context as parent
	then(parent.getParent()).isNotNull();

	ConfigurableApplicationContext bootstrapContext = (ConfigurableApplicationContext) parent
			.getParent();

	// Bootstrap should be the root, there should be no other parent
	then(bootstrapContext.getParent()).isNull();
}
 
Example 7
Source File: LiveBeansView.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Actually generate a JSON snapshot of the beans in the given ApplicationContexts.
 * <p>This implementation doesn't use any JSON parsing libraries in order to avoid
 * third-party library dependencies. It produces an array of context description
 * objects, each containing a context and parent attribute as well as a beans
 * attribute with nested bean description objects. Each bean object contains a
 * bean, scope, type and resource attribute, as well as a dependencies attribute
 * with a nested array of bean names that the present bean depends on.
 * @param contexts the set of ApplicationContexts
 * @return the JSON document
 */
protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
	StringBuilder result = new StringBuilder("[\n");
	for (Iterator<ConfigurableApplicationContext> it = contexts.iterator(); it.hasNext();) {
		ConfigurableApplicationContext context = it.next();
		result.append("{\n\"context\": \"").append(context.getId()).append("\",\n");
		if (context.getParent() != null) {
			result.append("\"parent\": \"").append(context.getParent().getId()).append("\",\n");
		}
		else {
			result.append("\"parent\": null,\n");
		}
		result.append("\"beans\": [\n");
		ConfigurableListableBeanFactory bf = context.getBeanFactory();
		String[] beanNames = bf.getBeanDefinitionNames();
		boolean elementAppended = false;
		for (String beanName : beanNames) {
			BeanDefinition bd = bf.getBeanDefinition(beanName);
			if (isBeanEligible(beanName, bd, bf)) {
				if (elementAppended) {
					result.append(",\n");
				}
				result.append("{\n\"bean\": \"").append(beanName).append("\",\n");
				result.append("\"aliases\": ");
				appendArray(result, bf.getAliases(beanName));
				result.append(",\n");
				String scope = bd.getScope();
				if (!StringUtils.hasText(scope)) {
					scope = BeanDefinition.SCOPE_SINGLETON;
				}
				result.append("\"scope\": \"").append(scope).append("\",\n");
				Class<?> beanType = bf.getType(beanName);
				if (beanType != null) {
					result.append("\"type\": \"").append(beanType.getName()).append("\",\n");
				}
				else {
					result.append("\"type\": null,\n");
				}
				result.append("\"resource\": \"").append(getEscapedResourceDescription(bd)).append("\",\n");
				result.append("\"dependencies\": ");
				appendArray(result, bf.getDependenciesForBean(beanName));
				result.append("\n}");
				elementAppended = true;
			}
		}
		result.append("]\n");
		result.append("}");
		if (it.hasNext()) {
			result.append(",\n");
		}
	}
	result.append("]");
	return result.toString();
}
 
Example 8
Source File: LiveBeansView.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Actually generate a JSON snapshot of the beans in the given ApplicationContexts.
 * <p>This implementation doesn't use any JSON parsing libraries in order to avoid
 * third-party library dependencies. It produces an array of context description
 * objects, each containing a context and parent attribute as well as a beans
 * attribute with nested bean description objects. Each bean object contains a
 * bean, scope, type and resource attribute, as well as a dependencies attribute
 * with a nested array of bean names that the present bean depends on.
 * @param contexts the set of ApplicationContexts
 * @return the JSON document
 */
protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
	StringBuilder result = new StringBuilder("[\n");
	for (Iterator<ConfigurableApplicationContext> it = contexts.iterator(); it.hasNext();) {
		ConfigurableApplicationContext context = it.next();
		result.append("{\n\"context\": \"").append(context.getId()).append("\",\n");
		if (context.getParent() != null) {
			result.append("\"parent\": \"").append(context.getParent().getId()).append("\",\n");
		}
		else {
			result.append("\"parent\": null,\n");
		}
		result.append("\"beans\": [\n");
		ConfigurableListableBeanFactory bf = context.getBeanFactory();
		String[] beanNames = bf.getBeanDefinitionNames();
		boolean elementAppended = false;
		for (String beanName : beanNames) {
			BeanDefinition bd = bf.getBeanDefinition(beanName);
			if (isBeanEligible(beanName, bd, bf)) {
				if (elementAppended) {
					result.append(",\n");
				}
				result.append("{\n\"bean\": \"").append(beanName).append("\",\n");
				result.append("\"aliases\": ");
				appendArray(result, bf.getAliases(beanName));
				result.append(",\n");
				String scope = bd.getScope();
				if (!StringUtils.hasText(scope)) {
					scope = BeanDefinition.SCOPE_SINGLETON;
				}
				result.append("\"scope\": \"").append(scope).append("\",\n");
				Class<?> beanType = bf.getType(beanName);
				if (beanType != null) {
					result.append("\"type\": \"").append(beanType.getName()).append("\",\n");
				}
				else {
					result.append("\"type\": null,\n");
				}
				result.append("\"resource\": \"").append(getEscapedResourceDescription(bd)).append("\",\n");
				result.append("\"dependencies\": ");
				appendArray(result, bf.getDependenciesForBean(beanName));
				result.append("\n}");
				elementAppended = true;
			}
		}
		result.append("]\n");
		result.append("}");
		if (it.hasNext()) {
			result.append(",\n");
		}
	}
	result.append("]");
	return result.toString();
}
 
Example 9
Source File: LiveBeansView.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Actually generate a JSON snapshot of the beans in the given ApplicationContexts.
 * <p>This implementation doesn't use any JSON parsing libraries in order to avoid
 * third-party library dependencies. It produces an array of context description
 * objects, each containing a context and parent attribute as well as a beans
 * attribute with nested bean description objects. Each bean object contains a
 * bean, scope, type and resource attribute, as well as a dependencies attribute
 * with a nested array of bean names that the present bean depends on.
 * @param contexts the set of ApplicationContexts
 * @return the JSON document
 */
protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
	StringBuilder result = new StringBuilder("[\n");
	for (Iterator<ConfigurableApplicationContext> it = contexts.iterator(); it.hasNext();) {
		ConfigurableApplicationContext context = it.next();
		result.append("{\n\"context\": \"").append(context.getId()).append("\",\n");
		if (context.getParent() != null) {
			result.append("\"parent\": \"").append(context.getParent().getId()).append("\",\n");
		}
		else {
			result.append("\"parent\": null,\n");
		}
		result.append("\"beans\": [\n");
		ConfigurableListableBeanFactory bf = context.getBeanFactory();
		String[] beanNames = bf.getBeanDefinitionNames();
		boolean elementAppended = false;
		for (String beanName : beanNames) {
			BeanDefinition bd = bf.getBeanDefinition(beanName);
			if (isBeanEligible(beanName, bd, bf)) {
				if (elementAppended) {
					result.append(",\n");
				}
				result.append("{\n\"bean\": \"").append(beanName).append("\",\n");
				result.append("\"aliases\": ");
				appendArray(result, bf.getAliases(beanName));
				result.append(",\n");
				String scope = bd.getScope();
				if (!StringUtils.hasText(scope)) {
					scope = BeanDefinition.SCOPE_SINGLETON;
				}
				result.append("\"scope\": \"").append(scope).append("\",\n");
				Class<?> beanType = bf.getType(beanName);
				if (beanType != null) {
					result.append("\"type\": \"").append(beanType.getName()).append("\",\n");
				}
				else {
					result.append("\"type\": null,\n");
				}
				result.append("\"resource\": \"").append(getEscapedResourceDescription(bd)).append("\",\n");
				result.append("\"dependencies\": ");
				appendArray(result, bf.getDependenciesForBean(beanName));
				result.append("\n}");
				elementAppended = true;
			}
		}
		result.append("]\n");
		result.append("}");
		if (it.hasNext()) {
			result.append(",\n");
		}
	}
	result.append("]");
	return result.toString();
}
 
Example 10
Source File: LiveBeansView.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Actually generate a JSON snapshot of the beans in the given ApplicationContexts.
 * <p>This implementation doesn't use any JSON parsing libraries in order to avoid
 * third-party library dependencies. It produces an array of context description
 * objects, each containing a context and parent attribute as well as a beans
 * attribute with nested bean description objects. Each bean object contains a
 * bean, scope, type and resource attribute, as well as a dependencies attribute
 * with a nested array of bean names that the present bean depends on.
 * @param contexts the set of ApplicationContexts
 * @return the JSON document
 */
protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
	StringBuilder result = new StringBuilder("[\n");
	for (Iterator<ConfigurableApplicationContext> it = contexts.iterator(); it.hasNext();) {
		ConfigurableApplicationContext context = it.next();
		result.append("{\n\"context\": \"").append(context.getId()).append("\",\n");
		if (context.getParent() != null) {
			result.append("\"parent\": \"").append(context.getParent().getId()).append("\",\n");
		}
		else {
			result.append("\"parent\": null,\n");
		}
		result.append("\"beans\": [\n");
		ConfigurableListableBeanFactory bf = context.getBeanFactory();
		String[] beanNames = bf.getBeanDefinitionNames();
		boolean elementAppended = false;
		for (String beanName : beanNames) {
			BeanDefinition bd = bf.getBeanDefinition(beanName);
			if (isBeanEligible(beanName, bd, bf)) {
				if (elementAppended) {
					result.append(",\n");
				}
				result.append("{\n\"bean\": \"").append(beanName).append("\",\n");
				String scope = bd.getScope();
				if (!StringUtils.hasText(scope)) {
					scope = BeanDefinition.SCOPE_SINGLETON;
				}
				result.append("\"scope\": \"").append(scope).append("\",\n");
				Class<?> beanType = bf.getType(beanName);
				if (beanType != null) {
					result.append("\"type\": \"").append(beanType.getName()).append("\",\n");
				}
				else {
					result.append("\"type\": null,\n");
				}
				result.append("\"resource\": \"").append(getEscapedResourceDescription(bd)).append("\",\n");
				result.append("\"dependencies\": [");
				String[] dependencies = bf.getDependenciesForBean(beanName);
				if (dependencies.length > 0) {
					result.append("\"");
				}
				result.append(StringUtils.arrayToDelimitedString(dependencies, "\", \""));
				if (dependencies.length > 0) {
					result.append("\"");
				}
				result.append("]\n}");
				elementAppended = true;
			}
		}
		result.append("]\n");
		result.append("}");
		if (it.hasNext()) {
			result.append(",\n");
		}
	}
	result.append("]");
	return result.toString();
}