直接在Servlet中獲得spring bean context

在web應用中通過以下方式裝載spring bean  context:

<context-param>
                     
<param-name>contextConfigLocation</param-name>
    
<param-value>/WEB-INF/classes/conf/appContext.xml</param-value>
</context-param>
        
<listener>
        
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  
</listener>

 在tapestry4下引入tapestry-spring.jar,即可在頁面中通過spring:訪問spring的bean。

當時在servlet中如何訪問這個spring context獲得bean呢?使用global嗎?

在網上找了資料也沒有找到。只是自己試驗出了一種方式,通過ServletContext訪問,暫時先用着:

ServletContext sc = req.getSession().getServletContext();
WebApplicationContext wc 
= WebApplicationContextUtils
                             .getWebApplicationContext(sc);

調用wc.getBean(beanID)就可以獲得對應的bean了

發佈了20 篇原創文章 · 獲贊 0 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章