posts - 14, comments - 15, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Spring web應用下方便的獲得bean(轉)

          Posted on 2009-01-06 13:27 析木 閱讀(1105) 評論(0)  編輯  收藏 所屬分類: Spring

                  如果我們的系統不是分布式的(在分布式里,我一般自己加載spring的配置文件),不是一般的application,通過自己加載Spring的配置文件的方式。而是一般的web應用,我們通過在web.xml里配置spring的配置文件。我們怎么方便的得到一個Bean的實例呢?當然,web應用啟動后,它已經創建好一個WebApplicationContext(這個是接口,其實也是ApplicationContext類型的,因為WebApplicationContext繼承自ApplictionContext這個接口)類型的實例對象,通過org.springframework.web.context.support.WebApplicationContextUtils里的
          getWebApplicationContext(ServletContext sc)可以得到這個對象的引用(這個就像我們一般的java application下得到ApplicationContext類型的引用一樣),我們就可以通過它的getBean方法得到我們的bean實例了。但是這里有個問題getWebApplicationContext(ServletContext sc)這個方法的參數ServletContext代表的是你web應用的環境,也就是說,也就是說web應用環境下特有的。這個時候如果你想得到一個bean的話,必須要有這個ServletContext對象存在,如果你每個類里都寫一個方法來接受ServletContext對象,從而得到WebApplicationContext類型實例的引用,之后再得到bean,進行你要的操作,這個是不是很麻煩?這個不是要寫很多代碼么?我覺得可以把獲得bean的這個操作的功能代碼放在一個Servlet里,讓這個Servlet在web應用啟動的時候加載,我們之后把這個Servlet當作普通類使用,調用里面的getBean方法就可以了。這個servlet的代碼如下:

           

          package com.nstc.cti.event;

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;

          import org.springframework.web.context.WebApplicationContext;
          import org.springframework.web.context.support.WebApplicationContextUtils;

          public class GetBean extends HttpServlet {

              
          private static WebApplicationContext context;

              
          public void init() throws ServletException {
                  context 
          = WebApplicationContextUtils.getWebApplicationContext(this
                          .getServletContext());
              }


              
          public static Object getBean(String id) {
                  Object bean 
          = context.getBean(id);
                  
          return bean;
              }

          }

          web.xml配置里:
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.5" 
              xmlns
          ="http://java.sun.com/xml/ns/javaee" 
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance" 
              xsi:schemaLocation
          ="http://java.sun.com/xml/ns/javaee 
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          >
           
          <!-- Spring的配置 -->
            
          <context-param>
                  
          <param-name>contextConfigLocation</param-name>
                  
          <param-value>/WEB-INF/beans.xml</param-value>
              
          </context-param>
              
          <servlet>
                  <servlet-name>springInitServlet</servlet-name>
                  <servlet-class>
                      org.springframework.web.context.ContextLoaderServlet
                  </servlet-class>
                  <load-on-startup>1</load-on-startup>
              </servlet>
            <servlet>

              
          <servlet-name>GetBeanServlet</servlet-name>
              
          <servlet-class>com.nstc.cti.event.GetBean</servlet-class>
                
          <load-on-startup>3</load-on-startup>
            
          </servlet>
             
          <!--Spring配置結束  -->
            
          <servlet-mapping>
              
          <servlet-name>GetBeanServlet</servlet-name>
              
          <url-pattern>/servlet/GetBeanServlet</url-pattern>
            
          </servlet-mapping>
           
            
          <welcome-file-list>
              
          <welcome-file>index.jsp</welcome-file>
            
          </welcome-file-list>
          </web-app>

          紅色也可以為:

              <listener>
                  
          <listener-class>
                      org.springframework.web.context.ContextLoaderListener
                  
          </listener-class>
              
          </listener>



          使用示例:

          假使我有一個Person的類,屬性有name和age,在spring配置文件里配置的id為“person”

          你在你的servlet或者jsp想得到這個實例的時候,直接這么做就可以了:

          Person person=(Person)GetBeanServlet.getBean("person");


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          我要啦免费统计
          主站蜘蛛池模板: 磴口县| 密山市| 唐海县| 武宣县| 虞城县| 祁门县| 福鼎市| 新密市| 南汇区| 宜兴市| 沂水县| 台中县| 宜君县| 兴国县| 卓资县| 金堂县| 公主岭市| 西青区| 甘泉县| 普陀区| 城固县| 高阳县| 都江堰市| 长治市| 鄂州市| 习水县| 深州市| 七台河市| 宁南县| 石屏县| 阿鲁科尔沁旗| 江陵县| 道孚县| 区。| 兖州市| 封开县| 睢宁县| 舟曲县| 灌阳县| 柳林县| 都安|