类加载应用程序上下文Spring [英] Class Loading Application Context Spring

查看:183
本文介绍了类加载应用程序上下文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"/>

请参阅 http://static.springsource.org/spring/docs/3.0.x/spring-framework -reference / html / beans.html#beans-factory-lifecycle-initializingbean 了解详情。

http://static.springsource.org/如果您更喜欢基于注释的配置,请使用spring / docs / 3.0.x / spring-framework-reference / html / beans.html#beans-postconstruct-and- predestroy-注释。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆