2010-03-04 90 views
2

這可能是由於我對Spring framewok的無知,但是我構建了一個JSF + Facelets + Spring web應用程序,我有一個bean,它的init方法我想在應用程序啓動的時間。我的問題是讓bean初始化。我可以在頁面上引用bean,當我進入頁面時,bean被初始化,並按照指示工作;我想反而是當應用程序啓動在web應用程序初始化的Spring bean啓動

是什麼讓一個Spring bean上的Web應用程序初始化的方法來初始化這個bean啓動

回答

1

所有的代碼,你想一個web應用程序初始化可以在實現了ServletContextListener爲

@WebListener 
public class ApplicationListener implements ServletContextListener {...} 

一類完成後立即處理,你可以創建Spring應用程序上下文如

ApplicationContext context = new ClassPathXmlApplicationContext(
     new String[] {"applicationContext.xml", "applicationContext-part2.xml"}); 
// of course, an ApplicationContext is just a BeanFactory 
BeanFactory factory = context; 

並獲得您感興趣的bean並繼續。

相關問題