[英]Spring Inheritance Autowired Annotations
For inheritance I see examples using XML Definition. 对于继承,我看到了使用XML定义的示例。 I had a doubt using Autowire and Annotations. 我对使用Autowire和注解有疑问。
I have 我有
@Component
class A{
}
@Component
class B extends A{
}
class TestClass{
@Autowire
A aObj;
}
So I believe this will inject Object of Class A. Correct ? 因此,我相信这将注入A类对象。对吗? Also If I make my class A as abstract, it will inject Class B object. 另外,如果我将A类设为抽象,它将注入B类对象。 Correct ? 对吗
Also it would be good if someone can give me a link to example for this. 如果有人可以给我链接到此示例,那也将是一件好事。
I tried the code and got the results as follows. 我尝试了代码,并得到了如下结果。
For above it throws NoUniqueBeanException. 上面它抛出NoUniqueBeanException。
If I make Class A as abstract it injects Class B Bean and works fine. 如果我将A类抽象化,它将注入B类Bean并可以正常工作。
If I don't want to make class A as abstract I need to Use Qualifiers as follows 如果我不想将A类抽象化,则需要使用Qualifiers,如下所示
@Component(value="aBean") class A{ } @Component(value="bBean") class B extends A{ } class TestClass{ @Autowire @Qualifier(value="aBean") A aObj; }
This injects Class A bean. 这将注入A类bean。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.