Spring Autowiring only works with Interface

前端 未结 6 902
慢半拍i
慢半拍i 2020-12-16 13:53

I am quite new to spring framework and came across the following issue.

I have an interface ClassA, which is implemented by classed ClassA1

相关标签:
6条回答
  • 2020-12-16 14:08

    I tried multiple ways to fix this problem, but I got it working the following way.

    @Autowired
    private ClassA classA1;
    
    @Autowired
    private ClassA classA2;
    

    In the application context, I defined the bean as below:

    <bean id="classA1" class="com.abc.ClassA1" autowire="byName" />
    <bean id="classA2" class="com.abc.ClassA2" autowire="byName" />
    
    0 讨论(0)
  • 2020-12-16 14:11

    I have similar problem with Autowiring abstract service. You can use without any problem code like this:

    @Autowired
    @Qualifier("classA1")
    private ClassA1 classA1;
    
    @Autowired
    @Qualifier("classA2")
    private ClassA2 classA2;
    

    This will be working only if you declare your bean like this

    <bean id="class1" class="com.abc.ClassA1" />
    

    Or like this

    @Component("classA1")
    public class ClassA1 {
    ...
    }
    
    0 讨论(0)
  • 2020-12-16 14:12

    From the little I've seen till now, it doesn't seem to be any restriction, regarding the type of class that one could mark as @Autowired.

    Non related to the issue, but this article makes reference to the situation itself

    0 讨论(0)
  • 2020-12-16 14:23

    For some reason your classes are proxied by Spring. There many reasons why this can happen. For example if you use JPA, or AOP the original class is proxied.

    If a class implements an interface, proxy means Dynamic Proxy. So basically a new class is created in runtime that implements the interfaces but does not inherit from the original class. Therefore the autowiring to the original class doesn't work.

    0 讨论(0)
  • 2020-12-16 14:23

    If your objects are proxied by JDK proxies, then they should be referred to by their interface. You can make proxies by concrete class using CGLIB (on the classpath) and proxy-target-class="true" in your aop configuration (in applicationContext.xml)

    0 讨论(0)
  • 2020-12-16 14:28

    You could use the @Qualifier annotation:

    @Autowired
    @Qualifier("class1")
    ClassA classA1;
    
    @Autowired
    @Qualifier("class2")
    ClassA classA2;
    

    Ref: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers

    or the @Resource annotation:

    @Resource(name="class1")
    ClassA classA1;
    
    @Resource(name="class2")
    ClassA classA2;
    

    Ref: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-resource-annotation

    0 讨论(0)
提交回复
热议问题