INTERFACES proxy mode and @Qualifier doesn't work

257 views Asked by At

Why I get an Exception when using @Qualifier and Interfaces proxy mode?

Error creating bean with name 'aopTest.TestBeanContainer': Unsatisfied dependency expressed through field 'beanA'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'pl.springui.components.HTMLRenderer' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=a), @org.springframework.beans.factory.annotation.Autowired(required=true)}

@EnableCaching
@Configuration
@ComponentScan(value = { "test.proxy" })
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AopTest {

    @Test
    public void testCache() {
        ApplicationContext context = new AnnotationConfigApplicationContext(AopTest.class);
        TestBeanContainer bean = context.getBean(TestBeanContainer.class);
        bean.test();
        bean.test();
    }

    @Component
    @Scope(value = "prototype")
    static class TestBeanContainer extends BaseBean {

        @Qualifier("a")
        @Autowired
        HTMLRenderer beanA;

        public void test() {
            BaseBean.print(beanA);
        }

        public TestBeanContainer() {
            super();
        }

    }

    @Qualifier("a")
    @Component
    @EqualsAndHashCode
    @Scope(value = "singleton", proxyMode = org.springframework.context.annotation.ScopedProxyMode.INTERFACES)
    class BeanA extends BaseBean implements HTMLRenderer {
    }

}

When I change @Qualifier to @Primary I don't get any exceptions but a new beanA is created when test() method is executed on the same object.

test executed:
NEW:BeanA                                          [id=5]   
    BeanA                                          [id=5]   |$Proxy27                                          
test executed:
NEW:BeanA                                         [id=6]   
    BeanA                                        [id=6]   |$Proxy27 
0

There are 0 answers