org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor Java Examples

The following examples show how to use org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor. 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: LazyAutowiredAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testLazyResourceInjectionWithNonExistingTarget() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(FieldResourceInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);

	FieldResourceInjectionBean bean = (FieldResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotNull(bean.getTestBean());
	try {
		bean.getTestBean().getName();
		fail("Should have thrown NoSuchBeanDefinitionException");
	}
	catch (NoSuchBeanDefinitionException ex) {
		// expected;
	}
}
 
Example #2
Source File: SpringBeanAutowiringSupport.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Process {@code @Autowired} injection for the given target object,
 * based on the current web application context.
 * <p>Intended for use as a delegate.
 * @param target the target object to process
 * @see org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext()
 */
public static void processInjectionBasedOnCurrentContext(Object target) {
	Assert.notNull(target, "Target object must not be null");
	WebApplicationContext cc = ContextLoader.getCurrentWebApplicationContext();
	if (cc != null) {
		AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
		bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());
		bpp.processInjection(target);
	}
	else {
		if (logger.isDebugEnabled()) {
			logger.debug("Current WebApplicationContext is not available for processing of " +
					ClassUtils.getShortName(target.getClass()) + ": " +
					"Make sure this class gets constructed in a Spring web application. Proceeding without injection.");
		}
	}
}
 
Example #3
Source File: LazyAutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testLazyOptionalResourceInjectionWithNonExistingTarget() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(OptionalFieldResourceInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);

	OptionalFieldResourceInjectionBean bean = (OptionalFieldResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotNull(bean.getTestBean());
	assertNotNull(bean.getTestBeans());
	assertTrue(bean.getTestBeans().isEmpty());
	try {
		bean.getTestBean().getName();
		fail("Should have thrown NoSuchBeanDefinitionException");
	}
	catch (NoSuchBeanDefinitionException ex) {
		// expected
	}
}
 
Example #4
Source File: LazyAutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testLazyResourceInjectionWithNonExistingTarget() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(FieldResourceInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);

	FieldResourceInjectionBean bean = (FieldResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotNull(bean.getTestBean());
	try {
		bean.getTestBean().getName();
		fail("Should have thrown NoSuchBeanDefinitionException");
	}
	catch (NoSuchBeanDefinitionException ex) {
		// expected
	}
}
 
Example #5
Source File: ConfigurationClassPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testCircularDependency() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	beanFactory.registerBeanDefinition("configClass1", new RootBeanDefinition(A.class));
	beanFactory.registerBeanDefinition("configClass2", new RootBeanDefinition(AStrich.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	try {
		beanFactory.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getMessage().contains("Circular reference"));
	}
}
 
Example #6
Source File: ConfigurationClassPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithFactoryBean() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryFactoryBeanInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryFactoryBeanConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.preInstantiateSingletons();

	RepositoryFactoryBeanInjectionBean bean = (RepositoryFactoryBeanInjectionBean) beanFactory.getBean("annotatedBean");
	assertSame(beanFactory.getBean("&repoFactoryBean"), bean.repositoryFactoryBean);
	assertSame(beanFactory.getBean("&repoFactoryBean"), bean.qualifiedRepositoryFactoryBean);
	assertSame(beanFactory.getBean("&repoFactoryBean"), bean.prefixQualifiedRepositoryFactoryBean);
}
 
Example #7
Source File: ConfigurationClassPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithImplTypeAtInjectionPoint() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(SpecificRepositoryInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(SpecificRepositoryConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.preInstantiateSingletons();

	SpecificRepositoryInjectionBean bean = (SpecificRepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertSame(beanFactory.getBean("genericRepo"), bean.genericRepository);
}
 
Example #8
Source File: ConfigurationClassPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithScopedProxyUsingAsm() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class.getName());
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ScopedProxyRepositoryConfiguration.class.getName()));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.freezeConfiguration();

	RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertEquals("Repository<String>", bean.stringRepository.toString());
	assertEquals("Repository<Integer>", bean.integerRepository.toString());
	assertTrue(AopUtils.isCglibProxy(bean.stringRepository));
	assertTrue(AopUtils.isCglibProxy(bean.integerRepository));
}
 
Example #9
Source File: ConfigurationClassPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithFactoryBean() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryFactoryBeanInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryFactoryBeanConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.preInstantiateSingletons();

	RepositoryFactoryBeanInjectionBean bean = (RepositoryFactoryBeanInjectionBean) beanFactory.getBean("annotatedBean");
	assertSame(beanFactory.getBean("&repoFactoryBean"), bean.repositoryFactoryBean);
	assertSame(beanFactory.getBean("&repoFactoryBean"), bean.qualifiedRepositoryFactoryBean);
	assertSame(beanFactory.getBean("&repoFactoryBean"), bean.prefixQualifiedRepositoryFactoryBean);
}
 
Example #10
Source File: ConfigurationClassPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithImplTypeAtInjectionPoint() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(SpecificRepositoryInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(SpecificRepositoryConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.preInstantiateSingletons();

	SpecificRepositoryInjectionBean bean = (SpecificRepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertSame(beanFactory.getBean("genericRepo"), bean.genericRepository);
}
 
Example #11
Source File: ConfigurationClassPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithScopedProxyUsingAsm() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class.getName());
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ScopedProxyRepositoryConfiguration.class.getName()));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.freezeConfiguration();

	RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertEquals("Repository<String>", bean.stringRepository.toString());
	assertEquals("Repository<Integer>", bean.integerRepository.toString());
	assertTrue(AopUtils.isCglibProxy(bean.stringRepository));
	assertTrue(AopUtils.isCglibProxy(bean.integerRepository));
}
 
Example #12
Source File: ConfigurationClassPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithScopedProxy() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ScopedProxyRepositoryConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.freezeConfiguration();

	RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertEquals("Repository<String>", bean.stringRepository.toString());
	assertEquals("Repository<Integer>", bean.integerRepository.toString());
	assertTrue(AopUtils.isCglibProxy(bean.stringRepository));
	assertTrue(AopUtils.isCglibProxy(bean.integerRepository));
}
 
Example #13
Source File: ConfigurationClassPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithScoped() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ScopedRepositoryConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);

	RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertEquals("Repository<String>", bean.stringRepository.toString());
	assertEquals("Repository<Integer>", bean.integerRepository.toString());
}
 
Example #14
Source File: ConfigurationClassPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void genericsBasedInjection() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);

	RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertEquals("Repository<String>", bean.stringRepository.toString());
	assertEquals("Repository<Integer>", bean.integerRepository.toString());
}
 
Example #15
Source File: ConfigurationClassPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithScopedProxy() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ScopedProxyRepositoryConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.freezeConfiguration();

	RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertEquals("Repository<String>", bean.stringRepository.toString());
	assertEquals("Repository<Integer>", bean.integerRepository.toString());
	assertTrue(AopUtils.isCglibProxy(bean.stringRepository));
	assertTrue(AopUtils.isCglibProxy(bean.integerRepository));
}
 
Example #16
Source File: ConfigurationClassPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testCircularDependency() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	beanFactory.registerBeanDefinition("configClass1", new RootBeanDefinition(A.class));
	beanFactory.registerBeanDefinition("configClass2", new RootBeanDefinition(AStrich.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	try {
		beanFactory.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getMessage().contains("Circular reference"));
	}
}
 
Example #17
Source File: SpringBeanAutowiringSupport.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Process {@code @Autowired} injection for the given target object,
 * based on the current web application context.
 * <p>Intended for use as a delegate.
 * @param target the target object to process
 * @see org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext()
 */
public static void processInjectionBasedOnCurrentContext(Object target) {
	Assert.notNull(target, "Target object must not be null");
	WebApplicationContext cc = ContextLoader.getCurrentWebApplicationContext();
	if (cc != null) {
		AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
		bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());
		bpp.processInjection(target);
	}
	else {
		if (logger.isDebugEnabled()) {
			logger.debug("Current WebApplicationContext is not available for processing of " +
					ClassUtils.getShortName(target.getClass()) + ": " +
					"Make sure this class gets constructed in a Spring web application. Proceeding without injection.");
		}
	}
}
 
Example #18
Source File: SpringBeanAutowiringSupport.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Process {@code @Autowired} injection for the given target object,
 * based on the current web application context.
 * <p>Intended for use as a delegate.
 * @param target the target object to process
 * @see org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext()
 */
public static void processInjectionBasedOnCurrentContext(Object target) {
	Assert.notNull(target, "Target object must not be null");
	WebApplicationContext cc = ContextLoader.getCurrentWebApplicationContext();
	if (cc != null) {
		AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
		bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());
		bpp.processInjection(target);
	}
	else {
		if (logger.isDebugEnabled()) {
			logger.debug("Current WebApplicationContext is not available for processing of " +
					ClassUtils.getShortName(target.getClass()) + ": " +
					"Make sure this class gets constructed in a Spring web application. Proceeding without injection.");
		}
	}
}
 
Example #19
Source File: LazyAutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testLazyOptionalResourceInjectionWithNonExistingTarget() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(OptionalFieldResourceInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);

	OptionalFieldResourceInjectionBean bean = (OptionalFieldResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotNull(bean.getTestBean());
	assertNotNull(bean.getTestBeans());
	assertTrue(bean.getTestBeans().isEmpty());
	try {
		bean.getTestBean().getName();
		fail("Should have thrown NoSuchBeanDefinitionException");
	}
	catch (NoSuchBeanDefinitionException ex) {
		// expected
	}
}
 
Example #20
Source File: LazyAutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testLazyResourceInjectionWithNonExistingTarget() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(FieldResourceInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);

	FieldResourceInjectionBean bean = (FieldResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotNull(bean.getTestBean());
	try {
		bean.getTestBean().getName();
		fail("Should have thrown NoSuchBeanDefinitionException");
	}
	catch (NoSuchBeanDefinitionException ex) {
		// expected
	}
}
 
Example #21
Source File: ConfigurationClassPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCircularDependency() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	beanFactory.registerBeanDefinition("configClass1", new RootBeanDefinition(A.class));
	beanFactory.registerBeanDefinition("configClass2", new RootBeanDefinition(AStrich.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	try {
		beanFactory.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getMessage().contains("Circular reference"));
	}
}
 
Example #22
Source File: SpringBeanAutowiringSupport.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Process {@code @Autowired} injection for the given target object,
 * based on the current web application context.
 * <p>Intended for use as a delegate.
 * @param target the target object to process
 * @see org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext()
 */
public static void processInjectionBasedOnCurrentContext(Object target) {
	Assert.notNull(target, "Target object must not be null");
	WebApplicationContext cc = ContextLoader.getCurrentWebApplicationContext();
	if (cc != null) {
		AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
		bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());
		bpp.processInjection(target);
	}
	else {
		if (logger.isDebugEnabled()) {
			logger.debug("Current WebApplicationContext is not available for processing of " +
					ClassUtils.getShortName(target.getClass()) + ": " +
					"Make sure this class gets constructed in a Spring web application. Proceeding without injection.");
		}
	}
}
 
Example #23
Source File: ConfigurationClassPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithScoped() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ScopedRepositoryConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);

	RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertEquals("Repository<String>", bean.stringRepository.toString());
	assertEquals("Repository<Integer>", bean.integerRepository.toString());
}
 
Example #24
Source File: ConfigurationClassPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void genericsBasedInjection() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);

	RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertEquals("Repository<String>", bean.stringRepository.toString());
	assertEquals("Repository<Integer>", bean.integerRepository.toString());
}
 
Example #25
Source File: ConfigurationClassPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithFactoryBean() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryFactoryBeanInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryFactoryBeanConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.preInstantiateSingletons();

	RepositoryFactoryBeanInjectionBean bean = (RepositoryFactoryBeanInjectionBean) beanFactory.getBean("annotatedBean");
	assertSame(beanFactory.getBean("&repoFactoryBean"), bean.repositoryFactoryBean);
	assertSame(beanFactory.getBean("&repoFactoryBean"), bean.qualifiedRepositoryFactoryBean);
	assertSame(beanFactory.getBean("&repoFactoryBean"), bean.prefixQualifiedRepositoryFactoryBean);
}
 
Example #26
Source File: ConfigurationClassPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithImplTypeAtInjectionPoint() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(SpecificRepositoryInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(SpecificRepositoryConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.preInstantiateSingletons();

	SpecificRepositoryInjectionBean bean = (SpecificRepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertSame(beanFactory.getBean("genericRepo"), bean.genericRepository);
}
 
Example #27
Source File: ConfigurationClassPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithScopedProxyUsingAsm() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class.getName());
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ScopedProxyRepositoryConfiguration.class.getName()));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.freezeConfiguration();

	RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertEquals("Repository<String>", bean.stringRepository.toString());
	assertEquals("Repository<Integer>", bean.integerRepository.toString());
	assertTrue(AopUtils.isCglibProxy(bean.stringRepository));
	assertTrue(AopUtils.isCglibProxy(bean.integerRepository));
}
 
Example #28
Source File: ConfigurationClassPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithScopedProxy() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ScopedProxyRepositoryConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.freezeConfiguration();

	RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertEquals("Repository<String>", bean.stringRepository.toString());
	assertEquals("Repository<Integer>", bean.integerRepository.toString());
	assertTrue(AopUtils.isCglibProxy(bean.stringRepository));
	assertTrue(AopUtils.isCglibProxy(bean.integerRepository));
}
 
Example #29
Source File: ConfigurationClassPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithScoped() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ScopedRepositoryConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);

	RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertEquals("Repository<String>", bean.stringRepository.toString());
	assertEquals("Repository<Integer>", bean.integerRepository.toString());
}
 
Example #30
Source File: ConfigurationClassPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void genericsBasedInjection() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);

	RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
	assertEquals("Repository<String>", bean.stringRepository.toString());
	assertEquals("Repository<Integer>", bean.integerRepository.toString());
}