【发布时间】:2020-01-22 22:24:06
【问题描述】:
我有多个带有@Component 实现通用接口的bean。
我有一个类,我使用这些 bean 中的方法。我没有单独自动装配每个 bean,而是想办法将接口自动装配为一个列表。
但是当我只是自动装配接口时,如何调用不同 bean 的方法。
public interface Generic {
}
@Component
public class A implements Generic{
.....
public void test{
}
}
@Component
public class B implements Generic{
.....
public void read{}
}
@component class C {
@Autowired
List <Generic> mylist; // type of list is Generic
现在我需要从 B 访问读取并从 A 进行测试
【问题讨论】:
-
使用
read()和test()方法的唯一方法是将它们转换为相关类(A或B),然后调用该方法。 -
@Saranya_R 你有调用“read()”和“test()”的顺序吗?
标签: spring list spring-boot interface autowired