Search in sources :

Example 11 with AutowiredAnnotationBeanPostProcessor

use of org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor in project spring-framework by spring-projects.

the class ConfigurationClassPostProcessorTests method genericsBasedInjectionWithScopedProxy.

@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));
}
Also used : AutowiredAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 12 with AutowiredAnnotationBeanPostProcessor

use of org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor in project spring-framework by spring-projects.

the class ConfigurationClassPostProcessorTests method genericsBasedInjection.

@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());
}
Also used : AutowiredAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 13 with AutowiredAnnotationBeanPostProcessor

use of org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor in project spring-framework by spring-projects.

the class ConfigurationClassPostProcessorTests method genericsBasedInjectionWithImplTypeAtInjectionPoint.

@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);
}
Also used : AutowiredAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 14 with AutowiredAnnotationBeanPostProcessor

use of org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor in project spring-framework by spring-projects.

the class SpringHandlerInstantiatorTests method setup.

@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();
}
Also used : AutowiredAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Before(org.junit.Before)

Example 15 with AutowiredAnnotationBeanPostProcessor

use of org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor in project spring-framework by spring-projects.

the class SpringBeanAutowiringSupport method processInjectionBasedOnServletContext.

/**
	 * 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);
}
Also used : AutowiredAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Aggregations

AutowiredAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor)16 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)14 Test (org.junit.Test)13 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 WebApplicationContext (org.springframework.web.context.WebApplicationContext)2 Before (org.junit.Before)1 ScopedObject (org.springframework.aop.scope.ScopedObject)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1