Q about AbstractApplicationContext.getBeansOfType() and getBean()

Posted by Paul Reiners on Stack Overflow See other posts from Stack Overflow or by Paul Reiners
Published on 2009-06-05T19:42:10Z Indexed on 2010/05/20 23:10 UTC
Read the original article Hit count: 186

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about spring

Related posts about java