简体   繁体   English

Spring 4-Autowire通用接口

[英]Spring 4 - Autowire generic interface

Starting with Spring (v4.3.8), I encounter a problem when Spring try to load dependency injections. 从Spring(v4.3.8)开始,当Spring尝试加载依赖项注入时遇到一个问题。

I want to call the "manage" method of the ManagerImpl1.java or ManagerImpl2.java implementation based on the type of T (Debit1 or Debit2). 我想基于T(Debit1或Debit2)的类型调用ManagerImpl1.java或ManagerImpl2.java实现的“管理”方法。 Here are the details: 详细信息如下:

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.xxx.xxx.datacollection.infoprod.Manager' available: expected single matching bean but found 2: ManagerImpl1,ManagerImpl2 由以下原因引起:org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有类型为'com.xxx.xxx.datacollection.infoprod.Manager'的合格Bean:预期为单个匹配的Bean,但是找到了2:ManagerImpl1,ManagerImpl2

Business.java : Business.java:

@Component
public class Business<T extends Debit> {

    @Autowired
    private Manager<T> manager;

    public void treatment(Context<T> context, FindServiceReturnMessage response) {

        manager.manage(response, context);
    }

Manager.java : Manager.java:

public interface Manager<T extends Debit> {

    void manage(final FindServiceReturnMessage response, Context<T> context);
}

ManagerImpl1.java : ManagerImpl1.java:

@Component
public class ManagerImpl1 implements Manager<Debit1> {

    @Override
    public void manage(final FindServiceReturnMessage response, Context<Debit1> context) {

    }
}

ManagerImpl2.java : ManagerImpl2.java:

@Component
public class ManagerImpl2 implements Manager<Debit2> {

    @Override
    public void manage(final FindServiceReturnMessage response, Context<Debit2> context) {

    }
}

Also, "Debit1" and "Debit2" implement the interface "Debit". 同样,“借方1”和“借方2”实现接口“借方”。

I tried several things but without success... 我尝试了几件事但没有成功...

You need to add a qualifier on your Bean like the following: 您需要在Bean上添加一个限定符,如下所示:

@Component
@Qualifier("managerImpl2")
public class ManagerImpl2 implements Manager<Debit2> {

    @Override
    public void manage(final FindServiceReturnMessage response, Context<Debit2> context) 
    {

    }
}

Then, when you wanna use it, use once more your @Qualifier annotation: 然后,当您要使用它时,请再次使用@Qualifier批注:

@Component
public class Business<T extends Debit> {

    @Autowired
    @Qualifier("managerImpl2")
    private Manager<T> manager;

    public void treatment(Context<T> context, FindServiceReturnMessage response) {

        manager.manage(response, context);
    }
}

Yet, as @dvorog said in the comments, as Generics are not present at compile, you will probably have to make a Business interface like this: 但是,正如@dvorog在评论中所说,由于泛型不存在于编译中,因此您可能必须创建一个如下的Business接口:

public interface Business<T extends Debit> {

    public void treatment(Context<T> context, FindServiceReturnMessage response);
}

and implements BusinessImpl to specify it each time you have a new kind of Debit : 并实现BusinessImpl以在每次有新的Debit时指定它:

@Component
public class ManagerImpl2Business<Debit2> {

    @Autowired
    @Qualifier("managerImpl2")
    private Manager<Debit2> manager;

    public void treatment(Context<Debit2> context, FindServiceReturnMessage response) {

        manager.manage(response, context);
    }
}

This will repeat a step over and will probably end with some unmaintainable code, or some if (T instanceof Debit1) somewhere to make your genericity work. 这将重复一遍,并可能以一些if (T instanceof Debit1)维护的代码或某些if (T instanceof Debit1) ,以使您的通用性正常工作。

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

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