乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      獲取spring的ApplicationContext幾種方式【轉(zhuǎn)】

       liang1234_ 2020-03-25

      轉(zhuǎn)自:http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html

      Java類獲取spring 容器的bean

      常用的5種獲取spring 中bean的方式總結(jié):

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

      代碼:

      1 ApplicationContext ac = new FileSystemXmlApplicationContext('applicationContext.xml'); 2 ac.getBean('beanId');

      說明:這種方式適用于采用Spring框架的獨立應(yīng)用程序,需要程序通過配置文件手工初始化Spring的情況。

      方法二:通過Spring提供的工具類獲取ApplicationContext對象
      代碼:

      復(fù)制代碼
      1 import org.springframework.web.context.support.WebApplicationContextUtils;
      2 ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
      3 ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
      4 ac1.getBean('beanId');
      5 ac2.getBean('beanId');
      復(fù)制代碼

      說明:這種方式適合于采用Spring框架的B/S系統(tǒng),通過ServletContext對象獲取ApplicationContext對象,然后在通過它獲取需要的類實例。

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

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

      方法四:繼承自抽象類WebApplicationObjectSupport
      說明:類似上面方法,調(diào)用getWebApplicationContext()獲取WebApplicationContext

      方法五:實現(xiàn)接口ApplicationContextAware
      說明:實現(xiàn)該接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 對象。
      Spring初始化時,會通過該方法將ApplicationContext對象注入。

      雖 然,spring提供了后三種方法可以實現(xiàn)在普通的類中繼承或?qū)崿F(xiàn)相應(yīng)的類或接口來獲取spring 的ApplicationContext對象,但是在使用是一定要注意實現(xiàn)了這些類或接口的普通java類一定要在Spring 的配置文件application-context.xml文件中進行配置。否則獲取的ApplicationContext對象將為null。



      如下是我實現(xiàn)了ApplicationContextAware接口的例子

      復(fù)制代碼
      1 package quartz.util; 2 3 import org.springframework.beans.BeansException; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.ApplicationContextAware; 6 7 public class SpringConfigTool implements ApplicationContextAware{//extends ApplicationObjectSupport{ 8 9 private static ApplicationContext context = null; 10 private static SpringConfigTool stools = null; 11 public synchronized static SpringConfigTool init(){ 12 if(stools == null){ 13 stools = new SpringConfigTool(); 14 } 15 return stools; 16 } 17 18 public void setApplicationContext(ApplicationContext applicationContext) 19 throws BeansException { 20 context = applicationContext; 21 } 22 23 public synchronized static Object getBean(String beanName) { 24 return context.getBean(beanName); 25 } 26 27 }
      復(fù)制代碼

      XML文件中的配置信息

      最后提供一種不依賴于servlet,不需要注入的方式
      注意一點,在服務(wù)器啟動時,Spring容器初始化時,不能通過以下方法獲取Spring 容器,如需細節(jié)可以觀看源碼

      復(fù)制代碼
      1 org.springframework.web.context.ContextLoader
      2 
      3 Title1 import org.springframework.web.context.ContextLoader; 
      4 import org.springframework.web.context.WebApplicationContext; 
      5  
      6 WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); 
      7   wac.getBean(beanID);
      復(fù)制代碼

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多