spring中擷取applicationContext

來源:互聯網
上載者:User

標籤:java   使用   檔案   art   代碼   io   

常用的5種擷取spring 中bean的方式總結:

方法一:在初始化時儲存ApplicationContext對象
代碼:
ApplicationContext ac = new FileSystemXmlApplicationContext("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。

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

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

方法五:實現介面ApplicationContextAware
說明:實現該介面的setApplicationContext(ApplicationContext context)方法,並儲存ApplicationContext 對象。
Spring初始化時,會通過該方法將ApplicationContext對象注入。

 

雖然,spring提供了後三種方法可以實現在普通的類中繼承或實現相應的類或介面來擷取spring 的ApplicationContext對象,但是在使用是一定要注意實現了這些類或介面的普通java類一定要在Spring 的設定檔application-context.xml檔案中進行配置。否則擷取的ApplicationContext對象將為null。

 

如下是我實現了ApplicationContextAware介面的例子

package quartz.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringConfigTool implements ApplicationContextAware{//extends ApplicationObjectSupport{
 
 private static ApplicationContext context = null;
 private static SpringConfigTool stools = null;
 public synchronized static SpringConfigTool init(){
  if(stools == null){
   stools = new SpringConfigTool();
  }
  return stools;
 }
 
 public void setApplicationContext(ApplicationContext applicationContext)
 throws BeansException {
  context = applicationContext;
 }

 public synchronized static Object getBean(String beanName) {
  return context.getBean(beanName);
 }

}

 

XML檔案中的配置資訊

<bean id="SpringConfigTool" class="quartz.util.SpringConfigTool"></bean>

最後提供一種不依賴於servlet,不需要注入的方式
注意一點,在伺服器啟動時,Spring容器初始化時,不能通過以下方法擷取Spring 容器,如需細節可以觀看源碼org.springframework.web.context.ContextLoader

Title1 import org.springframework.web.context.ContextLoader;  
2 import org.springframework.web.context.WebApplicationContext;  
3   
4     WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();  
5     wac.getBean(beanID);  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.