Spring工具類:WebApplicationContextUtils

今天看代碼,看到了需要Dao方法,但是沒有通過註解的方式注入,直接通過工具類.getBean('...Dao');獲取這個Dao類.

...Dao ...Dao = (...Dao) Util.getBean("...Dao");
public static Object getBean(String beanName) {
        ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(SystemListener.servletContext);
        return applicationContext.getBean(beanName);
    }

SystemListener.servletContext是系統初始化時加載.

public void contextInitialized(ServletContextEvent sce) {
    ServletContext servletContext = ServletContextEvent.getServletContext();
}

在已有的註解類型下,獲取WebApplicationContext的工具類

通過  WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContextEvent sce);可以獲得spring的單例webContext,這種方法是非常方便的,

WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContextEvent sce);

參數的sce怎麼拿到的呢?

可以通過ServletContextListener 接口實現拿到,而ServletContextListener 中的方法只有下邊兩個,一個項目啓動初始化環境時調用,一個項目摧毀關閉時調用,

其中,都可以剛給我們傳遞一個servletcontextevent這個請求上下文事件對象,我們可利用它來初始化一個WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContextEvent sce);中的sce對象,

這樣就齊活了。

我們的系統就是在初始化上寫的

public interface ServletContextListener extends EventListener {
 
    public abstract void contextInitialized(ServletContextEvent servletcontextevent);
 
    public abstract void contextDestroyed(ServletContextEvent servletcontextevent);
}

當 Web 應用集成 Spring 容器後,代表 Spring 容器的WebApplicationContext對象將以

WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 爲鍵存放在ServletContext的屬性列表中。您當然可以直接通過以下語句獲取 WebApplicationContext:

 

1

WebApplicationContext wac = (WebApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

但通過位於 org.springframework.web.context.support 包中的WebApplicationContextUtils 工具類獲取 WebApplicationContext 更方便:

1

2

3

4

ApplicationContext ac1 =WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);

ApplicationContext ac2 =WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);

ac1.getBean("beanId");

ac2.getBean("beanId");

說明:
這種方式適合於採用Spring框架的B/S系統,通過ServletContext對象獲取ApplicationContext對象,然後在通過它獲取需要的類實例。

上面兩個工具方式的區別是,前者在獲取失敗時拋出異常,後者返回null。

servletContext sc 換成

1.servlet.getServletContext()

2.this.getServletContext() 

3.request.getSession().getServletContext();

實例:

1

2

3

4

5

6

7

8

9

10

11

12

public class demoServlet extends HttpServlet {

 IDemoWS demoWS;

 public void init() throws ServletException {         

        super.init();

        ServletContext servletContext = this.getServletContext(); 

        WebApplicationContext ctx =WebApplicationContextUtils.getWebApplicationContext(servletContext);

        demoWS = (ISignpicWS)ctx.getBean("demoWS");

    }  

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  .....//request.getSession().getServletContext()

 }

}

參考:

https://www.cnblogs.com/luoruiyuan/p/5498407.html

https://www.cnblogs.com/luoruiyuan/p/5498442.html

https://blog.csdn.net/wuqilianga/article/details/60877802

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章