日期:2014-05-19  浏览次数:20729 次

Spring在应用程序中,如何初始化spring容器后不要关闭,始终运行。
我想再应用程序中集成hibernate和spring框架,但是spring初始化的时候每次都是要使用:
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");来获得bean。
这样会不会很浪费资源,有没有一个好的方法,一次初始化spring后,整个应用程序未关闭前,不结束spring容器的生命 周期,可以多次获得bean。在应用程序关闭后,随之关闭Spring容器。
在线等回答。。。。

------解决方案--------------------
public static ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
写进一个专门的类里。
以后直接用就行了。
------解决方案--------------------
探讨

public static ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
写进一个专门的类里。
以后直接用就行了。

------解决方案--------------------
这个跟spring的生命周期没关系吧。static声明的变量是整个aplication关闭之前都有效的哦。

我不知道你开始是怎么声明的static,又是怎么去调用的。