[英]Does Spring Context have equivalent of Java CDI's @Inject @Any
In Java CDI there is great functionality on gathering all managed beans, that share a supertype. 在Java CDI中,收集所有共享超类型的托管Bean具有强大的功能。 Let's say I what to gather all managed instances of MyInterceptor, I just have to write:
假设我要收集MyInterceptor的所有托管实例,我只需要写:
@Inject
@Any
Instance<MyInterceptor> myInterceptors;
Similar functionality I was able to obtain when I was working on EJB, although solution there was way less pretty (I needed to call BeanManager). 当我使用EJB时,我可以获得类似的功能,尽管解决方案的方式不太漂亮(我需要调用BeanManager)。
Can similar functionality I can obtain working on @Component's managed by Spring context? 我可以在Spring上下文管理的@Component上获得类似的功能吗?
[Edit] Why is not a duplicate of What is the Spring equivalent for CDI's Instance, or Guices Provider [编辑]为什么与CDI实例或“指导提供者”的Spring等效项不相同
From my understanding, in the above question, there is a hidden assumption, that I have both contexts: Spring and CDI, which may not be always the case (especially in Java SE applications). 根据我的理解,在上述问题中,有一个隐藏的假设,即我同时拥有两种上下文:Spring和CDI,但并非总是如此(尤其是在Java SE应用程序中)。 Furthermore, I have a bad experience in mixing contexts, they often claim that they are compatible, but it isn't always the case.
此外,我在混合上下文方面经验很差,他们经常声称它们是兼容的,但并非总是如此。 You can get trapped in unexpected problems, like this: https://blog.akquinet.de/2017/01/04/dont-get-trapped-into-a-memory-leak-using-cdi-instance-injection/
您可能会陷入意料之外的问题,例如: https : //blog.akquinet.de/2017/01/04/dont-get-trapped-into-a-memory-leak-using-cdi-instance-injection/
If you specify the the field as a List
with a generic type MyInterctptor
you should be able to achieve what you are looking for. 如果将字段指定为具有通用类型
MyInterctptor
的List
,则应该能够实现所需的功能。 Ex: 例如:
@Autowired
List<MyInterceptor> interceptors;
More info can be found here: https://dzone.com/articles/load-all-implementors 可以在这里找到更多信息: https : //dzone.com/articles/load-all-implementors
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.