我有一个 Spring Web 项目,我需要在初始化应用程序上下文后加载一些类,因为这些类最终将在将来使用。因此,我尝试在使用前预加载它们以提高性能。
怎么做 ?
请帮忙。
Thanks.
要将一个类加载到 JVM 中,只需调用Class.forName('com.foo.bar.MyClassToPreLoad')
方法。
你可以这样做,例如在你自己的实现中javax.servlet.ServletContextListener
然后在web.xml中注册
<listener>
<listener-class>com.foo.bar.MyClassPreloadingContextListener</listener-class>
</listener>
或者你可以在任何 Spring bean 中实现org.springframework.beans.factory.InitializingBean
界面。或者,如果您不想实现接口,您可以在任何不带参数的 bean 方法中执行此操作,并将其注册为初始化方法对于这个豆:
<bean class="com.foo.bar.MyClassPreloadingBean" init-method="preloadClasses"/>
See 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-factory-lifecycle-initializingbean了解详情。
Or http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-postconstruct-and-predestroy-annotations http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-postconstruct-and-predestroy-annotations如果您更喜欢基于注释的配置。
希望能帮助到你。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)