We have the following legacy 2.0.7 Spring code:
我们有如下遗留的2.0.7 Spring代码:
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.)
注意,我们忽略了getBeansOfType()的返回值。这很好,但问题是调用getBeansOfType()是很费时的。然而,即使我们忽略了这个调用的返回值,如果我们试图消除这个调用,那么getBean()返回的MyClass实例并没有完全初始化。(显然,getBeansOfType()的调用有一些我们需要的副作用。)
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).
我们怀疑调用getBeansOfType()是过度的,我们可以做一些更轻量级的事情,这样调用getBean()获得的MyClass实例将被完全初始化(但它不是null,也不会抛出异常)。
So, is there a more efficient way of doing this?
那么,有没有更有效的方法呢?
1 个解决方案
#1
1
First of all I suggest turning on logging and seeing what goes on. Spring is usually quite helpful.
首先,我建议打开日志,看看会发生什么。春天通常是很有帮助的。
Second of all, one difference between context.getBeansOfType
and context.getBean
is that getBeansOfType
does not query the parent context. If you have one, you might hit a difference here.
其次,上下文的一个区别。getBeansOfType和上下文。getBean是getBeansOfType不查询父上下文。如果你有一个,你可能会影响到这里。
Third of all, I assume the 'driver' bean is lazily initialised. Do you have any sort of multi-threaded code running while the context is initialized? There have been reports of fixes late in the 2.5 cycle related to that. getBeansOfType
might be just generating a delay or hitting a memory barrier so that the issue with getBean
returning an uninitialised bean is hidden.
第三,我假定“驱动程序”bean是惰性初始化的。在初始化上下文时,是否有任何类型的多线程代码运行?在与此相关的2.5个周期中,出现了修复的报告。getBeansOfType可能只是生成一个延迟或达到一个内存屏障,从而使getBean返回未初始化的bean的问题被隐藏。
Fourth of all, you might want to - just for argument's sake - try running the application with Spring 2.5.6. If it works, you know the guilty party.
第四,您可能想——只是为了讨论的缘故——尝试使用Spring 2.5.6运行应用程序。如果成功了,你知道有罪的一方。
#1
1
First of all I suggest turning on logging and seeing what goes on. Spring is usually quite helpful.
首先,我建议打开日志,看看会发生什么。春天通常是很有帮助的。
Second of all, one difference between context.getBeansOfType
and context.getBean
is that getBeansOfType
does not query the parent context. If you have one, you might hit a difference here.
其次,上下文的一个区别。getBeansOfType和上下文。getBean是getBeansOfType不查询父上下文。如果你有一个,你可能会影响到这里。
Third of all, I assume the 'driver' bean is lazily initialised. Do you have any sort of multi-threaded code running while the context is initialized? There have been reports of fixes late in the 2.5 cycle related to that. getBeansOfType
might be just generating a delay or hitting a memory barrier so that the issue with getBean
returning an uninitialised bean is hidden.
第三,我假定“驱动程序”bean是惰性初始化的。在初始化上下文时,是否有任何类型的多线程代码运行?在与此相关的2.5个周期中,出现了修复的报告。getBeansOfType可能只是生成一个延迟或达到一个内存屏障,从而使getBean返回未初始化的bean的问题被隐藏。
Fourth of all, you might want to - just for argument's sake - try running the application with Spring 2.5.6. If it works, you know the guilty party.
第四,您可能想——只是为了讨论的缘故——尝试使用Spring 2.5.6运行应用程序。如果成功了,你知道有罪的一方。