java 访问给定类型的所有 spring bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3896318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get access to all spring beans of a given type
提问by bwawok
I have a Spring application (Spring Batch not web application). In a test class, I want to grab access to all of my beans of a given type.
I understand that in Spring you should generally use IOC and let the container inject your beans. However in this case, I want to loop through a variable number of beans that extend a given class (org.springframework.batch.item.database.JdbcCursorItemReader), and do something (want it to be a unit/integration test that just connects it to the database and reads 1 row, so we can confirm at test time that all of the JdbcCursorItemReader in the system have valid SQL and row mappers).
我有一个 Spring 应用程序(Spring Batch 不是 Web 应用程序)。在测试类中,我想获取对给定类型的所有 bean 的访问权限。
我知道在 Spring 中你通常应该使用 IOC 并让容器注入你的 bean。但是在这种情况下,我想遍历扩展给定类(org.springframework.batch.item.database.JdbcCursorItemReader)的可变数量的bean,并做一些事情(希望它是一个单元/集成测试,只连接它到数据库并读取 1 行,因此我们可以在测试时确认系统中的所有 JdbcCursorItemReader 都具有有效的 SQL 和行映射器)。
Problem 1) I can only get beans one at a time. I can have my class implement BeanFactoryAwareto get a reference to my beanfactory. Then I can do beanFactory.getBean("name");to get access to a single bean. How do I instead get ALL beans? I can loop through and drop the ones that aren't the class I want.. but somehow I need a list of all beans the beanfactory knows about or something.
问题 1) 我一次只能得到一个豆子。我可以让我的类实现 BeanFactoryAware以获取对我的 beanfactory 的引用。然后我可以做beanFactory.getBean("name"); 访问单个bean。我如何才能获得所有豆子?我可以循环并删除不是我想要的类的那些..但不知何故我需要一个 beanfactory 知道的所有 bean 的列表或其他东西。
Problem 2) The bean I get back from the beanfactory is a proxy. If I try to cast and use my bean I get something like java.lang.ClassCastException: $Proxy0 cannot be cast to org.springframework.batch.item.database.JdbcCursorItemReader
问题 2)我从 beanfactory 得到的 bean 是一个代理。如果我尝试强制转换并使用我的 bean,我会得到类似 java.lang.ClassCastException: $Proxy0 can be cast to org.springframework.batch.item.database.JdbcCursorItemReader 的内容
回答by skaffman
You can get around the first problem by using ApplicationContextAware
instead of BeanFactoryAware
. This will pass in the ApplicationContext
, which has the getBeansOfType()
method which lets you retrieve all beans that are of a given type.
您可以通过使用ApplicationContextAware
代替 来解决第一个问题BeanFactoryAware
。这将传入ApplicationContext
,它具有getBeansOfType()
可让您检索给定类型的所有 bean的方法。
The second problem is likely caused because something is creating AOP proxies around your JdbcCursorItemReader
bean. These generated proxies will, by default, implement the same interfaces that JdbcCursorItemReader
does (specifically, ItemReader
and ItemStream
). Your code should not try and cast to the class type (JdbcCursorItemReader
), but to one of those interface types instead. It's usually possible to force the proxy to extend the proxied class directly, but without knowing anything about your setup, I can't help you with that.
第二个问题很可能是因为某些东西在你的JdbcCursorItemReader
bean周围创建了 AOP 代理。默认情况下,这些生成的代理将实现与JdbcCursorItemReader
(特别是ItemReader
和ItemStream
)相同的接口。您的代码不应尝试转换为类类型 ( JdbcCursorItemReader
),而应转换为这些接口类型之一。通常可以强制代理直接扩展代理类,但是在不了解您的设置的情况下,我无法帮助您。