春天的Autowire诠释抽象类:没有独特的bean定义 [英] Spring Autowire Annotation on Abstract class: No unique bean is defined

查看:577
本文介绍了春天的Autowire诠释抽象类:没有独特的bean定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类:

@Component
public abstract class BaseReport {

  public void export() {
   ...
}

和一堆扩展它,并覆盖export()方法(或没有)类。

And a bunch of classes that extend it, and override the export() method (or not).

@Component
public final class Report1 extends BaseReport

@Component
public final class Report2 extends BaseReport

我的大多数测试自动装配具体的类扩展BaseReport,没有任何问题:

Most of my tests autowire concrete classes that extend BaseReport, with no problems:

public class Report1Test extends BaseTest {

    @Autowired
    Report1 _report;

public class Report2Test extends BaseTest {

    @Autowired
    Report2 _report;

这工作得很好了的所有类的自动装配延长的BaseReport。但我也需要自动装配的抽象类本身,BaseReport,测试export()方法。

This works fine for autowiring of all classes that extend BaseReport. But I also need to autowire the abstract class itself, BaseReport, to test the export() method.

public class BaseReportTest extends BaseTest {

  @Autowired
  BaseReport _report;

当我尝试运行它,我得到了臭名昭著的:

When I try to run it I get the infamous:

类型BaseReport没有独特的豆定义:预期单个匹配的bean,但发现2 [报表1,报告2]

我已经使用@Qualifier尝试,但与@Qualifier的问题是,(据我所知),你用它来告诉Spring哪个类 - 实现一个接口或扩展一个抽象类 - 你希望使用。但是,这不是我的情况。我想用抽象类本身。

I've tried using @Qualifier but the problem with @Qualifier is that (as I understand it) you use it to tell Spring which class -- that implements an Interface or extends an Abstract class - you wish to use. But that's not my case. I want to use the abstract class itself.

我使用@Resource,这样也试过:

I also tried using @Resource, like this:

public class BaseReportTest extends BaseTest {

  @Resource(name = "baseReport")
  BaseReport _report;

春季告诉我有这个名字没有豆。 (

Spring tells me there is no bean with this name. :(

我怎样才能做到这一点?

How can I do this?

干杯。

推荐答案

抽象类不能被实例化,你需要使用一个具体的实现。
同在普通的Java,如果你试图实例化一个抽象类,它会告诉你实现内的抽象方法。当你这样做时,将创建一个匿名类。这不是抽象类的instatiation,但抽象类的新的子类。

Abstract classes can't be instantiated, you need to use a concrete implementation. Same as in regular java, if you try to instantiate an abstract class, it tells you to implement the abstract methods within. When you do, an anonymous class is created. It's not an instatiation of the abstract class, but a new subclass of that abstract class.

Spring就会查找延伸你的基类的类,作为报表1和报告2,
春认为它具有符合要求的,不知道该选哪一个,多个类别。因此,你的错误,有多个匹配的bean。

Spring will look for classes which extend your base class, being Report1 and Report2, Spring sees it has multiple classes which match the requirements and doesn't know which one to choose. thus you get the error that there are multiple matching beans.

您可以通过一个适配器解决这个问题basicly创建延伸您的基类,实现了抽象方法,但什么都不做他们的具体类。然后,你可以自动装配反对它的实施和测试。但是你的抽象类应媒体链接进行测试由于如果错误仍与基类出现你正在测试报告1和2的事实,这意味着你不使用导致的错误,这是一个不好的做法,反正逻辑。还带有测试工具coveage你可以发现未使用的code的方式。

You can fix this by making an "adapter" basicly create a concrete class which extends your base-class, implements the abstract methods, but doesn't do anything them. Then you can autowire that implementation and test against it. However your abstract class should allready be tested due to the fact you are testing report 1 and 2. If errors still occur with your base class, it means logic you don't use is causing bugs, which is a bad practice anyway. also with a test coveage tool you could spot unused code that way.

这篇关于春天的Autowire诠释抽象类:没有独特的bean定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆