类加载应用程序上下文Spring

我有一个Spring Web项目,我需要在初始化应用程序上下文后加载一些类,因为这些类最终将在未来使用。 因此,我尝试在使用前预加载它们以提高性能。

怎么做 ?

请帮忙。

谢谢。

要将类加载到JVM中,只需调用Class.forName('com.foo.bar.MyClassToPreLoad')方法就足够了。 您可以在自己的javax.servlet.ServletContextListener实现中执行此操作,然后在web.xml中注册它

  com.foo.bar.MyClassPreloadingContextListener  

或者您可以在任何实现org.springframework.beans.factory.InitializingBean接口的Spring bean中执行此操作。 或者,如果您不想实现接口,可以在没有参数的任何bean方法中执行它,并将其注册为此bean的init方法

  

有关详细信息,请参阅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-annotations如果您更喜欢基于注释的配置。

希望能帮助到你。

我想你还没有提到你的bean的范围。如果你没有在应用程序上下文中提到范围,那么默认容器使用singleton scope.It意味着在整个urs系统中使用相同的bean实例,除非你关闭容器。 bean保持总是一样的。如果你想覆盖默认行为,可以在urs applicationcontext中为bean提供范围。你最好在这个链接中看到我曾经问过同一个问题的问题。 使用tomcat在spring中预加载和延迟加载