简体   繁体   English

Spring @Autowire 继承

[英]Spring @Autowire with Inheritance

I am new to Spring and wanted your all assistance.我是 Spring 的新手,希望得到您的所有帮助。 I have run into the "expected single match bean but found two" exception.我遇到了“预期的单个匹配 bean,但发现两个”异常。 I have searched for the solution and think have understood the solution.我已经搜索了解决方案并认为已经理解了解决方案。 Most solutions suggest using @Qualifier to resolve this exception.大多数解决方案建议使用@Qualifier 来解决此异常。 However I don't think it will solve my issue.但是我认为它不会解决我的问题。 Below is my class hierarchy:下面是我的类层次结构:

abstract class A{

    @Autowired
    Client client;                     

    protected void doSomething(){ 
        /* ....some code .... */
        client.someStuff(); 
        /* ....some code .... */
    }

    /* ..... few abstract methods ......... */
}

class B extends A{ 
    public void action(){ doSomething() }
}

class C extends A{
    public void action(){ doSomething() }
}

My .xml confirugration file is我的 .xml 配置文件是

    <bean id="authClientA"  class="com.xyz.Client">
        <property name="auth" value="abc">
    </bean>


    <bean id="authClientB"  class="com.xyz.Client">
        <property name="auth" value="xyz">
    </bean>

    <bean id="beanA"  class="ClassA">
        <property name="client"  ref="authClientA">
    </bean>

    <bean id="beanB"  class="ClassB">
        <property name="auth" ref="authClientB">
    </bean>

So basically, I have an abstract class which has a method which will be common to both the subclasses.所以基本上,我有一个抽象类,它有一个对两个子类都通用的方法。 This method uses the client reference but at run time will use different object.此方法使用客户端引用,但在运行时将使用不同的对象。 doSomething() is not overridden in the subclasses. doSomething()在子类中没有被覆盖。

So when I try to run this code, it gives me the exception for client reference.因此,当我尝试运行此代码时,它给了我客户端引用的异常。 I dont think I can use @Qualifier because @Qualifier requires to specify bean name from .xml as parameter, but since the 'client' attribute is common, I cant use only one bean name.我不认为我可以使用@Qualifier,因为@Qualifier 需要将 .xml 中的 bean 名称指定为参数,但是由于 'client' 属性很常见,我不能只使用一个 bean 名称。

Can someone please assist in explaining if there is a way to get around.有人可以帮忙解释一下是否有办法绕过。 Duplicating the doSomething() method defeats the entire purpose of inheritance and will cause duplicate code across classes.复制doSomething()方法违背了继承的全部目的,并且会导致跨类的代码重复。 I can't have the client attribute in the subclasses as it will become unknown to doSomething() method at compile time.我不能在子类中拥有client属性,因为在编译时doSomething()方法将不知道它。

Any ideas/suggestions/solutions will be helpful.任何想法/建议/解决方案都会有所帮助。

Thanks in advance :)提前致谢 :)

@Qualifier is the option, you are probably not able to use it. @Qualifier是选项,您可能无法使用它。

Modifying your XML:修改您的 XML:

<bean id="authClientA"  class="com.xyz.Client">
    <property name="auth" value="abc">
    <qualifier value="clientA" />
</bean>


<bean id="authClientB"  class="com.xyz.Client">
    <property name="auth" value="xyz">
    <qualifier value="clientB" />
</bean>

<bean id="beanA"  class="ClassA">
    <property name="client"  ref="authClientA">
</bean>

<bean id="beanB"  class="ClassB">
    <property name="auth" ref="authClientB">
</bean>

And fixing your class:并修复您的课程:

abstract class A{

      @Qualifier("clientA")
      Client clientA;                     

      @Qualifier("clientB")
      Client clientB;       

      protected void doSomething(){ 
               /* ....some code .... */
               client.someStuff(); /* HERE YOU NEED SOME LOGIC TO INVOKCE THE CORRECT OBJECT METHOD */
               /* ....some code .... */
      }

      /* ..... few abstract methods * ........./
}

Also, if you want to understand more, please check this answer here :另外,如果您想了解更多信息,请在此处查看此答案:

Spring Autowire Annotation on Abstract class: No unique bean is defined 抽象类上的 Spring Autowire 注释:未定义唯一的 bean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM