为什么在使用@Qualifier
和Interfaces代理模式时会出现异常?
创建名为'aopTest.TestBeanContainer'的bean时出错:不满意 通过字段'beanA'表示的依赖;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 'pl.springui.components.HTMLRenderer'类型的限定bean 可用:预计至少有1个符合autowire资格的bean 候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Qualifier(值= a)中, @ org.springframework.beans.factory.annotation.Autowired(所需=真)}
@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 {
}
}
当我将@Qualifier
更改为@Primary
时,我没有得到任何异常,但在同一对象上执行test()
方法时会创建一个新的beanA。
test executed:
NEW:BeanA [id=5]
BeanA [id=5] |$Proxy27
test executed:
NEW:BeanA [id=6]
BeanA [id=6] |$Proxy27