java Spring Autowiring 仅适用于接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6511276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:09:30  来源:igfitidea点击:

Spring Autowiring only works with Interface

javaspringautowired

提问by Chaitanya

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

我对 spring 框架很陌生,遇到了以下问题。

I have an interface ClassA, which is implemented by classed ClassA1and ClassA2.

我有一个接口ClassA,它是由 classed ClassA1and实现的ClassA2

I have the following bean definition added to applicationContext.xml

我添加了以下 bean 定义 applicationContext.xml

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

I would like to Autowire both the implementation classes as below.

我想自动装配两个实现类,如下所示。

@Autowired
private ClassA1 classA1;

@Autowired
private ClassA2 classA2;

The above code is throwing error as

上面的代码抛出错误为

Could not autowrite to field: com.abc.ClassA1 com.abc.SomeClass.classA1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.abc.ClassA1]

无法自动写入字段:com.abc.ClassA1 com.abc.SomeClass.classA1;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.abc.ClassA1]

But, if I change the autowiring to interface as below:

但是,如果我将自动装配更改为如下界面:

@Autowired
ClassA classA1;

Then ClassA1 is autowired to the variable. I am clueless on how can I autowire a variable to ClassA2.

然后 ClassA1 自动连接到变量。我对如何将变量自动连接到 ClassA2 一无所知。

采纳答案by Chaitanya

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 定义如下:

<bean id="classA1" class="com.abc.ClassA1" autowire="byName" />
<bean id="classA2" class="com.abc.ClassA2" autowire="byName" />

回答by Tarlog

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.

出于某种原因,您的课程由 Spring 代理。发生这种情况的原因有很多。例如,如果您使用 JPA 或 AOP,则代理原始类。

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.

如果一个类实现了一个接口,代理就是动态代理。因此,基本上在运行时创建了一个新类,它实现了接口但不从原始类继承。因此,自动装配到原始类不起作用。

回答by Bozho

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)

如果您的对象由 JDK 代理代理,那么它们应该由它们的接口引用。您可以使用 CGLIB(在类路径上)和proxy-target-class="true"在您的 aop 配置(在applicationContext.xml)中通过具体类创建代理

回答by matsev

You could use the @Qualifierannotation:

您可以使用@Qualifier注释:

@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

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

or the @Resourceannotation:

@Resource注释:

@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

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

回答by Raph

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 时,这才有效

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

Or like this

或者像这样

@Component("classA1")
public class ClassA1 {
...
}

回答by thodorisbais

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.

从我到现在为止看到的一点点,关于可以标记为@Autowired 的类的类型,它似乎没有任何限制。

Non related to the issue, but thisarticle makes reference to the situation itself

不相关的问题,但这个文章提到的情况本身