Search in sources :

Example 1 with AutowiredAnnotationBeanPostProcessor

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

the class LazyAutowiredAnnotationBeanPostProcessorTests method testLazyResourceInjectionWithNonExistingTarget.

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

Example 2 with AutowiredAnnotationBeanPostProcessor

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

the class ConfigurationClassPostProcessorTests method testSelfReferenceExclusionForFactoryMethodOnSameBean.

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

Example 3 with AutowiredAnnotationBeanPostProcessor

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

the class ConfigurationClassPostProcessorTests method genericsBasedInjectionWithFactoryBean.

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

Example 4 with AutowiredAnnotationBeanPostProcessor

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

the class ConfigurationClassPostProcessorTests method genericsBasedInjectionWithScoped.

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

Example 5 with AutowiredAnnotationBeanPostProcessor

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

the class ConfigurationClassPostProcessorTests method genericsBasedInjectionWithScopedProxyUsingAsm.

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

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