I think that not.
For Spring, you could use:
@Inject
private Map<String, Foo> fooBeans;
to avoid explicit reference to ApplicationContex
, but AFAIK this will not work with others DI containers.
题
I'm trying more and more to design and build my applications such that they are framework agnostic. This means using JSR annotations instead of Spring annotations, JPA2 interfaces, etc. but I find myself having a lot of difficulty at times.
For example, at the moment, I need to retrieve a list of all beans of a particular class. Using a Spring-specific method, I can use the ApplicationContext to .getBeansOfType(Clazz)
, but I'm trying to do this without using the Spring ApplicationContext.
Is there a JSR equivalent to Spring's application context?
Ex (quick psuedo code):
@Autowired private ApplicationContext ctx;
protected Map<String, Clazz> getBeans(){
return ctx.getBeansOfType( Clazz );
}
How can I do this without using Spring?
解决方案
I think that not.
For Spring, you could use:
@Inject
private Map<String, Foo> fooBeans;
to avoid explicit reference to ApplicationContex
, but AFAIK this will not work with others DI containers.
其他提示
You won't find a framework agnostic way of doing this. For CDI you'd use
@Inject
Instance<Foo> fooBeans;