Java普通類獲取Spring框架Bean 的五種方法

          Posted on 2010-03-15 13:44 無很 閱讀(10054) 評論(0)  編輯  收藏 所屬分類: Spring

          方法一:在初始化時保存ApplicationContext對象

          代碼:
          ApplicationContext ac = new FileSystemXmlApplicationContex("applicationContext.xml");
              ac.getBean("beanId");
             
          說明:
             
          這種方式適用于采用Spring框架的獨立應用程序,需要程序通過配置文件手工初始化Spring的情況。
           
          方法二:通過Spring提供的工具類獲取ApplicationContext對象
          代碼:
           import org.springframework.web.context.support.WebApplicationContextUtils;
          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 可 以具體 換成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext();

          另外,由于spring是注入的對象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 對象:

          WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
                  
           
          方法三:繼承自抽象類ApplicationObjectSupport
             
          說明:
              
          抽象類ApplicationObjectSupport提供getApplicationContext()方法,可以方便的獲取到ApplicationContextSpring初始化時,會通過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象注入。

           

          方法四:繼承自抽象類WebApplicationObjectSupport
             
          說明:
                  
          類似上面方法,調用getWebApplicationContext()獲取WebApplicationContext
           
          方法五:實現接口ApplicationContextAware
            
          說明:
              
          實現該接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 對象。Spring初始化時,會通過該方法將ApplicationContext 對象注入。
           
          以上方法適合不同的情況,請根據具體情況選用相應的方法。
          這里值得提一點的是,系統中用到上述方法的類實際上就于Spring框架緊密耦合在一起了,因為這些類是知道它們是運行在Spring框架上的,因此,系統中,應該盡量的減少這類應用,使系統盡可能 的獨立于當前運行環境,盡量通過DI的方式獲取需要的服務提供者。

           

               然 后在Action中 編寫如下代碼得到Context,(我是覆蓋了Struts ActionsetServlet方法,也許還有更好的方法)。

               public void setServlet(ActionServlet servlet){
                   super.setServlet(servlet);
                   ServletContext servletContext = servlet.getServletContext();
                  WebApplicationContext wac =    WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
                   // get yours beans

               }


          我需要在springbean中直接獲取,這下可和我們常規的操作有些不同,因為springbean都是pojo的。根本見不著servletconfigservletcontext的影子。

            這里我 需要指出spring給 我們提供了兩個接口:org.springframework.web.context.ServletContextAware

          org.springframework.web.context.ServletConfigAware。我們可以讓我們的bean實現上邊的任何一個接口就能獲取到servletContext .

          代碼如下:

          public class DicBean implements ServletContextAware{
              
          private ServletContext servletContext;

          public void setServletContext(ServletContext sc) {
              this.servletContext=sc;
              System.out.println("項目的絕對路徑為:"+servletContext.getRealPath("/"));
          }
          }
          主站蜘蛛池模板: 永安市| 洛浦县| 孝感市| 盐边县| 长乐市| 天气| 抚松县| 兴业县| 久治县| 东阿县| 平定县| 长丰县| 都昌县| 蕲春县| 绥德县| 文化| 社旗县| 台前县| 会泽县| 南投县| 久治县| 油尖旺区| 综艺| 乐山市| 云南省| 兴仁县| 云霄县| 龙江县| 卢龙县| 扎囊县| 泰宁县| 子洲县| 湖北省| 永春县| 新乐市| 乡城县| 塘沽区| 黄梅县| 昌平区| 胶南市| 益阳市|