Java Code Examples for org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#setBeanFactory()

The following examples show how to use org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#setBeanFactory() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
Source File: ConfigurationClassPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void scopedProxyTargetMarkedAsNonAutowireCandidate() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(ScopedProxyConfigurationClass.class));
	beanFactory.registerBeanDefinition("consumer", new RootBeanDefinition(ScopedProxyConsumer.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);

	ITestBean injected = beanFactory.getBean("consumer", ScopedProxyConsumer.class).testBean;
	assertTrue(injected instanceof ScopedObject);
	assertSame(beanFactory.getBean("scopedClass"), injected);
	assertSame(beanFactory.getBean(ITestBean.class), injected);
}
 
Example 12
Source File: ConfigurationClassPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testConfigWithDefaultMethods() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	beanFactory.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ConcreteConfigWithDefaultMethods.class));
	beanFactory.registerBeanDefinition("serviceBeanProvider", new RootBeanDefinition(ServiceBeanProvider.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	beanFactory.preInstantiateSingletons();

	beanFactory.getBean(ServiceBean.class);
}
 
Example 13
Source File: UserRegistrationSteps.java    From user-registration-V2 with Apache License 2.0 5 votes vote down vote up
public UserRegistrationSteps() {
	super();
	SpringApplication application = new SpringApplication(
			RegistrationApplication.class);
	application.setWebEnvironment(false);
	AutowiredAnnotationBeanPostProcessor autowiredAnnotationBeanPostProcessor = new AutowiredAnnotationBeanPostProcessor();
	autowiredAnnotationBeanPostProcessor.setBeanFactory(application.run()
			.getBeanFactory());
	autowiredAnnotationBeanPostProcessor.processInjection(this);
}
 
Example 14
Source File: ConfigurationClassPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testSelfReferenceExclusionForFactoryMethodOnSameBean() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	beanFactory.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ConcreteConfig.class));
	beanFactory.registerBeanDefinition("serviceBeanProvider", new RootBeanDefinition(ServiceBeanProvider.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	beanFactory.preInstantiateSingletons();

	beanFactory.getBean(ServiceBean.class);
}
 
Example 15
Source File: SpringHandlerInstantiatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition("capitalizer", new RootBeanDefinition(Capitalizer.class));
	instantiator = new SpringHandlerInstantiator(bf);
	objectMapper = Jackson2ObjectMapperBuilder.json().handlerInstantiator(instantiator).build();
}
 
Example 16
Source File: ConfigurationClassPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testConfigWithDefaultMethodsUsingAsm() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	beanFactory.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ConcreteConfigWithDefaultMethods.class.getName()));
	beanFactory.registerBeanDefinition("serviceBeanProvider", new RootBeanDefinition(ServiceBeanProvider.class.getName()));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	beanFactory.preInstantiateSingletons();

	beanFactory.getBean(ServiceBean.class);
}
 
Example 17
Source File: SpringHandlerInstantiatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition("capitalizer", new RootBeanDefinition(Capitalizer.class));
	instantiator = new SpringHandlerInstantiator(bf);
	objectMapper = Jackson2ObjectMapperBuilder.json().handlerInstantiator(instantiator).build();
}
 
Example 18
Source File: ConfigurationClassPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void scopedProxyTargetMarkedAsNonAutowireCandidate() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(ScopedProxyConfigurationClass.class));
	beanFactory.registerBeanDefinition("consumer", new RootBeanDefinition(ScopedProxyConsumer.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);

	ITestBean injected = beanFactory.getBean("consumer", ScopedProxyConsumer.class).testBean;
	assertTrue(injected instanceof ScopedObject);
	assertSame(beanFactory.getBean("scopedClass"), injected);
	assertSame(beanFactory.getBean(ITestBean.class), injected);
}
 
Example 19
Source File: SpringBeanAutowiringSupport.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Process {@code @Autowired} injection for the given target object,
 * based on the current root web application context as stored in the ServletContext.
 * <p>Intended for use as a delegate.
 * @param target the target object to process
 * @param servletContext the ServletContext to find the Spring web application context in
 * @see WebApplicationContextUtils#getWebApplicationContext(javax.servlet.ServletContext)
 */
public static void processInjectionBasedOnServletContext(Object target, ServletContext servletContext) {
	Assert.notNull(target, "Target object must not be null");
	WebApplicationContext cc = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());
	bpp.processInjection(target);
}
 
Example 20
Source File: SpringBeanAutowiringSupport.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Process {@code @Autowired} injection for the given target object,
 * based on the current root web application context as stored in the ServletContext.
 * <p>Intended for use as a delegate.
 * @param target the target object to process
 * @param servletContext the ServletContext to find the Spring web application context in
 * @see WebApplicationContextUtils#getWebApplicationContext(javax.servlet.ServletContext)
 */
public static void processInjectionBasedOnServletContext(Object target, ServletContext servletContext) {
	Assert.notNull(target, "Target object must not be null");
	WebApplicationContext cc = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());
	bpp.processInjection(target);
}