Java Code Examples for org.springframework.context.annotation.AnnotationConfigUtils#registerAnnotationConfigProcessors()

The following examples show how to use org.springframework.context.annotation.AnnotationConfigUtils#registerAnnotationConfigProcessors() . 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: InjectAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutowiredConstructorArgumentResolvesQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedConstructorArgumentTestBean bean =
			(QualifiedConstructorArgumentTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example 2
Source File: InjectAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutowiredFieldWithMultipleNonQualifiedCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedFieldTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example 3
Source File: QualifierAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void autowiredMethodParameterResolvesQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedMethodParameterTestBean bean =
			(QualifiedMethodParameterTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example 4
Source File: QualifierAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void autowiredFieldResolvesMetaQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(MetaQualifiedFieldTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	MetaQualifiedFieldTestBean bean = (MetaQualifiedFieldTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example 5
Source File: InjectAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutowiredConstructorArgumentWithSingleNonQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
	context.registerBeanDefinition(JUERGEN, person);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example 6
Source File: QualifierAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void autowiredConstructorArgumentWithMultipleNonQualifiedCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example 7
Source File: InjectAnnotationAutowireContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutowiredConstructorArgumentResolvesQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedConstructorArgumentTestBean bean =
			(QualifiedConstructorArgumentTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example 8
Source File: InjectAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutowiredConstructorArgumentWithSingleNonQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
	context.registerBeanDefinition(JUERGEN, person);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example 9
Source File: QualifierAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void autowiredFieldResolvesWithDefaultValueAndExplicitDefaultValueOnBeanDefinition() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	// qualifier added, and value matches the default
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifierWithDefaultValue.class, "default"));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedFieldWithDefaultValueTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedFieldWithDefaultValueTestBean bean =
			(QualifiedFieldWithDefaultValueTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example 10
Source File: QualifierAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void autowiredFieldResolvesWithBaseQualifierAndDefaultValue() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	person2.addQualifier(new AutowireCandidateQualifier(Qualifier.class));
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedFieldWithBaseQualifierDefaultValueTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedFieldWithBaseQualifierDefaultValueTestBean bean =
			(QualifiedFieldWithBaseQualifierDefaultValueTestBean) context.getBean("autowired");
	assertEquals(MARK, bean.getPerson().getName());
}
 
Example 11
Source File: InjectAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutowiredMethodParameterWithMultipleNonQualifiedCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example 12
Source File: QualifierAnnotationAutowireContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutowiredMethodParameterWithMultipleNonQualifiedCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example 13
Source File: QualifierAnnotationAutowireContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutowiredFieldDoesNotResolveCandidateWithDefaultValueAndConflictingValueOnBeanDefinition() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	// qualifier added, and non-default value specified
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifierWithDefaultValue.class, "not the default"));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedFieldWithDefaultValueTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example 14
Source File: QualifierAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void autowiredFieldWithSingleNonQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
	context.registerBeanDefinition(JUERGEN, person);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedFieldTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example 15
Source File: InjectAnnotationAutowireContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutowiredFieldResolvesQualifiedCandidateWithDefaultValueAndNoValueOnBeanDefinition() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	// qualifier added, but includes no value
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifierWithDefaultValue.class));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedFieldWithDefaultValueTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedFieldWithDefaultValueTestBean bean =
			(QualifiedFieldWithDefaultValueTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example 16
Source File: InjectAnnotationAutowireContextTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutowiredFieldDoesNotResolveWithMultipleQualifierValuesAndConflictingDefaultValue() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(TestQualifierWithMultipleAttributes.class);
	qualifier.setAttribute("number", 456);
	person1.addQualifier(qualifier);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	AutowireCandidateQualifier qualifier2 = new AutowireCandidateQualifier(TestQualifierWithMultipleAttributes.class);
	qualifier2.setAttribute("number", 123);
	qualifier2.setAttribute("value", "not the default");
	person2.addQualifier(qualifier2);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedFieldWithMultipleAttributesTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example 17
Source File: ApplicationContextExpressionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void prototypeCreationReevaluatesExpressions() {
	GenericApplicationContext ac = new GenericApplicationContext();
	AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
	GenericConversionService cs = new GenericConversionService();
	cs.addConverter(String.class, String.class, new Converter<String, String>() {
		@Override
		public String convert(String source) {
			return source.trim();
		}
	});
	ac.getBeanFactory().registerSingleton(GenericApplicationContext.CONVERSION_SERVICE_BEAN_NAME, cs);
	RootBeanDefinition rbd = new RootBeanDefinition(PrototypeTestBean.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.getPropertyValues().add("country", "#{systemProperties.country}");
	rbd.getPropertyValues().add("country2", new TypedStringValue("-#{systemProperties.country}-"));
	ac.registerBeanDefinition("test", rbd);
	ac.refresh();

	try {
		System.getProperties().put("name", "juergen1");
		System.getProperties().put("country", " UK1 ");
		PrototypeTestBean tb = (PrototypeTestBean) ac.getBean("test");
		assertEquals("juergen1", tb.getName());
		assertEquals("UK1", tb.getCountry());
		assertEquals("-UK1-", tb.getCountry2());

		System.getProperties().put("name", "juergen2");
		System.getProperties().put("country", "  UK2  ");
		tb = (PrototypeTestBean) ac.getBean("test");
		assertEquals("juergen2", tb.getName());
		assertEquals("UK2", tb.getCountry());
		assertEquals("-UK2-", tb.getCountry2());
	}
	finally {
		System.getProperties().remove("name");
		System.getProperties().remove("country");
	}
}
 
Example 18
Source File: ConfigurationClassProcessingTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test  // SPR-11830
public void configWithSetWithProviderImplementation() {
	GenericApplicationContext ac = new GenericApplicationContext();
	AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
	ac.registerBeanDefinition("config", new RootBeanDefinition(ConfigWithSetWithProviderImplementation.class));
	ac.refresh();
	assertSame(ac.getBean("customName"), ConfigWithSetWithProviderImplementation.set);
}
 
Example 19
Source File: AbstractGenericContextLoader.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Load a Spring ApplicationContext from the supplied {@code locations}.
 *
 * <p>Implementation details:
 *
 * <ul>
 * <li>Creates a {@link GenericApplicationContext} instance.</li>
 * <li>Calls {@link #prepareContext(GenericApplicationContext)} to allow for customizing the context
 * before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory(DefaultListableBeanFactory)} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #createBeanDefinitionReader(GenericApplicationContext)} to create a
 * {@link BeanDefinitionReader} which is then used to populate the context
 * from the specified locations.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@link AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext(GenericApplicationContext)} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 *
 * <p><b>Note</b>: this method does not provide a means to set active bean definition
 * profiles for the loaded context. See {@link #loadContext(MergedContextConfiguration)}
 * and {@link AbstractContextLoader#prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)}
 * for an alternative.
 *
 * @return a new application context
 * @see org.springframework.test.context.ContextLoader#loadContext
 * @see GenericApplicationContext
 * @see #loadContext(MergedContextConfiguration)
 * @since 2.5
 */
@Override
public final ConfigurableApplicationContext loadContext(String... locations) throws Exception {
	if (logger.isDebugEnabled()) {
		logger.debug(String.format("Loading ApplicationContext for locations [%s].",
			StringUtils.arrayToCommaDelimitedString(locations)));
	}
	GenericApplicationContext context = new GenericApplicationContext();
	prepareContext(context);
	customizeBeanFactory(context.getDefaultListableBeanFactory());
	createBeanDefinitionReader(context).loadBeanDefinitions(locations);
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	customizeContext(context);
	context.refresh();
	context.registerShutdownHook();
	return context;
}
 
Example 20
Source File: AbstractGenericWebContextLoader.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Load a Spring {@link WebApplicationContext} from the supplied
 * {@link MergedContextConfiguration}.
 * <p>Implementation details:
 * <ul>
 * <li>Calls {@link #validateMergedContextConfiguration(WebMergedContextConfiguration)}
 * to allow subclasses to validate the supplied configuration before proceeding.</li>
 * <li>Creates a {@link GenericWebApplicationContext} instance.</li>
 * <li>If the supplied {@code MergedContextConfiguration} references a
 * {@linkplain MergedContextConfiguration#getParent() parent configuration},
 * the corresponding {@link MergedContextConfiguration#getParentApplicationContext()
 * ApplicationContext} will be retrieved and
 * {@linkplain GenericWebApplicationContext#setParent(ApplicationContext) set as the parent}
 * for the context created by this method.</li>
 * <li>Delegates to {@link #configureWebResources} to create the
 * {@link MockServletContext} and set it in the {@code WebApplicationContext}.</li>
 * <li>Calls {@link #prepareContext} to allow for customizing the context
 * before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #loadBeanDefinitions} to populate the context
 * from the locations or classes in the supplied {@code MergedContextConfiguration}.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@linkplain AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 * @return a new web application context
 * @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration)
 * @see GenericWebApplicationContext
 */
@Override
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
	Assert.isTrue(mergedConfig instanceof WebMergedContextConfiguration,
			() -> String.format("Cannot load WebApplicationContext from non-web merged context configuration %s. " +
					"Consider annotating your test class with @WebAppConfiguration.", mergedConfig));

	WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig;

	if (logger.isDebugEnabled()) {
		logger.debug(String.format("Loading WebApplicationContext for merged context configuration %s.",
			webMergedConfig));
	}

	validateMergedContextConfiguration(webMergedConfig);

	GenericWebApplicationContext context = new GenericWebApplicationContext();

	ApplicationContext parent = mergedConfig.getParentApplicationContext();
	if (parent != null) {
		context.setParent(parent);
	}
	configureWebResources(context, webMergedConfig);
	prepareContext(context, webMergedConfig);
	customizeBeanFactory(context.getDefaultListableBeanFactory(), webMergedConfig);
	loadBeanDefinitions(context, webMergedConfig);
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	customizeContext(context, webMergedConfig);
	context.refresh();
	context.registerShutdownHook();
	return context;
}