SpringBoot自动装配通用类型失败,因为可能有多个bean [英] SpringBoot Autowiring a generic type fails because of multiple possible beans

查看:1017
本文介绍了SpringBoot自动装配通用类型失败,因为可能有多个bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个类,以自动装配T类型的对象.

I'm trying to create a class that Autowire an object of type T.

@component    
public class TaskScheduler<T extends TaskService>{
    @Autowired
    private T taskService;
}

问题是我有两个扩展TaskService的组件.

the problem is that I have two components that extend TaskService.

@component
public class firstTaskService extends TaskService {

}

@component
public class secondTaskService extends TaskService {

}

因此,当执行此行时(正在创建ts)

so when this line is executed (ts is being created)

@Autowired
TaskScheduler<firstTaskService> ts;

我收到此错误:

说明: TaskScheduler中构造函数的参数1需要一个bean,但是找到了2个

Description: Parameter 1 of constructor in TaskScheduler required a single bean, but 2 were found

我得到的提示是这样的:

the message I got suggested this :

操作:考虑将一个bean标记为@Primary,更新使用者以接受多个bean,或使用@Qualifier进行标识 应该消耗的咖啡豆.

Action: Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed.

但是据我了解,@Primary@Qualifier注释使我选择了其中的一种组件,这不是我想要的组件,因为我想在同一类中使用firstTaskServicesecondTaskService().

But from what I understood, the @Primary and @Qualifier annotations make me choose 1 of the components, which not what I want because I want to use firstTaskService and secondTaskService with that same class (TaskScheduler).

这怎么办?

澄清:我的目标是将TaskScheduler类与扩展TaskService类的不同类重用(不要在TaskScheduler中使用一起扩展TaskService的多个类) ).

Clarification: My objective is to reuse the TaskScheduler class with different classes that extend the TaskService class (not to use multiple classes that extend TaskService together in TaskScheduler).

推荐答案

如果要对所有扩展了TaskService的bean进行自动装配,也许应该将自动装配的字段更改为List:

If you want to autowire all beans that extends TaskService maybe you should change the autowired field to a List:

@Component    
public class TaskScheduler<T extends TaskService>{
    @Autowired
    private List<T> taskService;
}

通过这种方式,Spring应该将扩展了TaskService的所有可自动连接的bean放入List中.

In this way Spring should put in the List all autowireable beans that extends TaskService.

编辑:由于您想动态选择TaskService的类型,因此我发现的唯一方法如下.首先,重新定义您的TaskScheduler:

EDIT: since you want to dinamically select the type of TaskService the only way I've found is the following. First, redefine your TaskScheduler:

public class TaskScheduler <T extends TaskService>{

    private T taskService;

    public void setTaskService(T taskService) {
        this.taskService = taskService;
    }
}

您的TaskService和相关子类应保持不变.按如下所示设置配置类:

Your TaskService and related subclasses should remain untouched. Set up a configuration class as it follows:

@Configuration
public class TaskConf {

    @Autowired
    private FirstTaskService firstTaskService;

    @Autowired
    private SecondTaskService secondTaskService;

    @Bean
    public TaskScheduler<FirstTaskService> firstTaskServiceTaskScheduler(){
        TaskScheduler<FirstTaskService> t =  new TaskScheduler<>();
        t.setTaskService(firstTaskService);
        return t;
    }

    @Bean
    public TaskScheduler<SecondTaskService> secondTaskServiceTaskScheduler(){
        TaskScheduler<SecondTaskService> t =  new TaskScheduler<>();
        t.setTaskService(secondTaskService);
        return t;
    }

}

然后以这种方式测试您的TaskScheduler:

And then test your TaskScheduler in this way:

@Autowired
TaskScheduler<firstTaskService> ts;

这篇关于SpringBoot自动装配通用类型失败,因为可能有多个bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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