Java普通類(lèi)獲取Spring框架Bean 的五種方法

          Posted on 2010-03-15 13:44 無(wú)很 閱讀(10054) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Spring

          方法一:在初始化時(shí)保存ApplicationContext對(duì)象

          代碼:
          ApplicationContext ac = new FileSystemXmlApplicationContex("applicationContext.xml");
              ac.getBean("beanId");
             
          說(shuō)明:
             
          這種方式適用于采用Spring框架的獨(dú)立應(yīng)用程序,需要程序通過(guò)配置文件手工初始化Spring的情況。
           
          方法二:通過(guò)Spring提供的工具類(lèi)獲取ApplicationContext對(duì)象
          代碼:
           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");
           
          說(shuō)明:
               
          這種方式適合于采用Spring框架的B/S系統(tǒng),通過(guò)ServletContext對(duì)象獲取ApplicationContext對(duì)象,然后在通過(guò)它獲取需要的類(lèi)實(shí)例。上面兩個(gè)工具方式的區(qū)別 是,前者在獲取失敗時(shí)拋出異常,后者返回null


          其 中 servletContext sc 可 以具體 換成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext();

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

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

           

          方法四:繼承自抽象類(lèi)WebApplicationObjectSupport
             
          說(shuō)明:
                  
          類(lèi)似上面方法,調(diào)用getWebApplicationContext()獲取WebApplicationContext
           
          方法五:實(shí)現(xiàn)接口ApplicationContextAware
            
          說(shuō)明:
              
          實(shí)現(xiàn)該接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 對(duì)象。Spring初始化時(shí),會(huì)通過(guò)該方法將ApplicationContext 對(duì)象注入。
           
          以上方法適合不同的情況,請(qǐng)根據(jù)具體情況選用相應(yīng)的方法。
          這里值得提一點(diǎn)的是,系統(tǒng)中用到上述方法的類(lèi)實(shí)際上就于Spring框架緊密耦合在一起了,因?yàn)檫@些類(lèi)是知道它們是運(yùn)行在Spring框架上的,因此,系統(tǒng)中,應(yīng)該盡量的減少這類(lèi)應(yīng)用,使系統(tǒng)盡可能 的獨(dú)立于當(dāng)前運(yùn)行環(huán)境,盡量通過(guò)DI的方式獲取需要的服務(wù)提供者。

           

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

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

               }


          我需要在springbean中直接獲取,這下可和我們常規(guī)的操作有些不同,因?yàn)?/span>springbean都是pojo的。根本見(jiàn)不著servletconfigservletcontext的影子。

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

          org.springframework.web.context.ServletConfigAware。我們可以讓我們的bean實(shí)現(xiàn)上邊的任何一個(gè)接口就能獲取到servletContext .

          代碼如下:

          public class DicBean implements ServletContextAware{
              
          private ServletContext servletContext;

          public void setServletContext(ServletContext sc) {
              this.servletContext=sc;
              System.out.println("項(xiàng)目的絕對(duì)路徑為:"+servletContext.getRealPath("/"));
          }
          }
          主站蜘蛛池模板: 延寿县| 永嘉县| 女性| 桐梓县| 玉龙| 屯昌县| 缙云县| 临城县| 金阳县| 绩溪县| 图片| 苏尼特左旗| 抚顺县| 吴堡县| 普陀区| 会昌县| 临海市| 垦利县| 竹溪县| 宜都市| 南昌市| 西乌珠穆沁旗| 平原县| 乐平市| 桐庐县| 综艺| 佳木斯市| 乌什县| 佛教| 盐亭县| 望奎县| 宜川县| 十堰市| 健康| 武平县| 武胜县| 左权县| 沈丘县| 马公市| 敦化市| 黄石市|