简体   繁体   English

有没有一种方法可以获取通过@Autowired注入的所有带有范围原型的bean?

[英]Is there a way to get all beans with scope prototype that are injected via @Autowired?

Injecting bean with scope prototype with @Autowired usually doesn't work as expected. @Autowired用作用域prototype注入bean通常不能按预期方式工作。 But when writing code, it's easy to accidentally inject a prototype. 但是在编写代码时,很容易意外注入原型。

Is there a way to get a list of all @Autowired fields and methods and to match that with a Spring AppContext to check for this? 有没有办法获取所有@Autowired字段和方法的列表,并将其与Spring AppContext进行匹配以进行检查?

One approach could be to override org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor (which is responsible for processing @Autowired, @Inject, @Resource etc) and perform the checks that you have mentioned in this overridden bean post processor. 一种方法是重写org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor (负责处理@ Autowired,@ Inject,@ Resource等),并执行在此重写的bean后处理器中提到的检查。 However, AutowiredAnnotationBeanPostProcessor gets registered with quite a few of the common custom namespaces ( context:component-scan , context:annotation-config etc), so these custom annotations will have to be replaced with the corresponding bean variation and the overridden post processor also registered as a bean. 但是,AutowiredAnnotationBeanPostProcessor已向相当多的通用自定义名称空间( context:component-scancontext:annotation-config等)注册,因此这些自定义注释将必须替换为相应的bean变体,并且也覆盖了重写的后处理器作为豆。

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

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