Q about AbstractApplicationContext.getBeansOfType() and getBean()
- by Paul Reiners
We have the following legacy 2.0.7 Spring code:
final Map<String, MyClass> secondaryFactories
= (Map<String, MyClass>) context.getBeansOfType(MyClass.class,
false, true);
return (MyClass) context.getBean("myClass");
where context is an instance of
org.springframework.context.support.AbstractApplicationContext
Note that we ignore the return value of getBeansOfType(). This works just fine, but the problem is that the call to getBeansOfType() is time-consuming. However, even though we ignore the return value of this call, if we try to eliminate this call, then the instance of MyClass returned by getBean() is not fully initialized. (So, apparently, the call to getBeansOfType() is having some sort of side-effects that we need.)
We suspect that the call to getBeansOfType() is overkill and we could do something more lightweight so that the instance of MyClass obtained by the call to getBean() would be fully initialized (but it's not null and no exception is thrown).
So, is there a more efficient way of doing this?