类加载应用程序上下文Spring [英] Class Loading Application Context Spring
问题描述
我有一个Spring web项目,我需要在应用程序上下文初始化后加载一些类,因为这些类最终将在未来使用。因此,我尝试在使用之前预加载它们以提高性能。
I have a Spring web project and I need load some classes after application context has been initialized because those classes will eventually used in future. Thus, I try to preload them before use to improve performance.
如何操作?
请帮助。
谢谢。
推荐答案
要将类加载到JVM中,只需调用 Class.forName foo.bar.MyClassToPreLoad')
方法。
你可以这样做。在您自己的实现 javax.servlet.ServletContextListener
,然后注册到web.xml
To load a class into JVM it is enough simply to call Class.forName('com.foo.bar.MyClassToPreLoad')
method.
You can do it e.g. in your own implementation of javax.servlet.ServletContextListener
and then register it in web.xml
<listener>
<listener-class>com.foo.bar.MyClassPreloadingContextListener</listener-class>
</listener>
或者你可以在你的任何Spring bean中实现 org.springframework .beans.factory.InitializingBean
接口。或者如果你不想实现接口,你可以在没有参数的任何bean方法中做,并注册为这个bean的 init-method :
Or you can do it in any of your Spring beans implementing org.springframework.beans.factory.InitializingBean
interface. Or if you don't want to implement interface you can do it in any bean method without arguments and register it as a init-method for this bean:
<bean class="com.foo.bar.MyClassPreloadingBean" init-method="preloadClasses"/>
Or http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-postconstruct-and-predestroy-annotations if you prefer annotation based configuration.
希望它有帮助。
这篇关于类加载应用程序上下文Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!