欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring在代碼中獲取bean的幾種方式詳解

 更新時(shí)間:2019年08月30日 08:19:16   作者:Mr.Aaron  
這篇文章主要介紹了Spring在代碼中獲取bean的幾種方式詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

方法如下

  • 方法一:通過讀取XML文件反射生成對(duì)象
  • 方法二:通過Spring提供的utils類獲取ApplicationContext對(duì)象
  • 方法三:繼承自抽象類ApplicationObjectSupport
  • 方法四:繼承自抽象類WebApplicationObjectSupport
  • 方法五:實(shí)現(xiàn)接口ApplicationContextAware
  • 方法六:通過Spring提供的ContextLoader

獲取spring中bean的方式總結(jié):

方法一:通過讀取XML文件反射生成對(duì)象

ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("userService");//比如:<bean id="userService" class="com.cloud.service.impl.UserServiceImpl"></bean>

說明:這樣的方式適用于採(cǎi)用Spring框架的獨(dú)立應(yīng)用程序,須要程序通過配置文件手工初始化Spring的情況。

方法二:通過Spring提供的工具類獲取ApplicationContext對(duì)象

ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");

說明:這樣的方式適合于Spring框架的B/S系統(tǒng),通過ServletContext對(duì)象獲取ApplicationContext對(duì)象。然后在通過它獲取須要的類實(shí)例。上面兩個(gè)工具方式的差別是,前者在獲取失敗時(shí)拋出異常。后者返回null。

方法三:繼承自抽象類ApplicationObjectSupport

說明:抽象類ApplicationObjectSupport提供getApplicationContext()方法。能夠方便的獲取ApplicationContext。

Spring初始化時(shí)。會(huì)通過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對(duì)象注入。

方法四:繼承自抽象類WebApplicationObjectSupport

說明:類似上面方法。調(diào)用getWebApplicationContext()獲取WebApplicationContext

方法五:實(shí)現(xiàn)接口ApplicationContextAware

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

下面是實(shí)現(xiàn)ApplicationContextAware接口方式的代碼,前面兩種方法類似:

public class SpringContextUtil implements ApplicationContextAware { 
 
  // Spring應(yīng)用上下文環(huán)境 
  private static ApplicationContext applicationContext; 
 
  /** 
   * 實(shí)現(xiàn)ApplicationContextAware接口的回調(diào)方法。設(shè)置上下文環(huán)境 
   * 
   * @param applicationContext 
   */ 
  public void setApplicationContext(ApplicationContext applicationContext) { 
    SpringContextUtil.applicationContext = applicationContext; 
  } 
 
  /** 
   * @return ApplicationContext 
   */ 
  public static ApplicationContext getApplicationContext() { 
    return applicationContext; 
  }  
  /** 
   * 獲取對(duì)象 
   * 
   * @param name 
   * @return Object
   * @throws BeansException 
   */ 
  public static Object getBean(String name) throws BeansException { 
    return applicationContext.getBean(name); 
  } 
}

盡管,spring提供的后三種方法能夠?qū)嵢缃衿胀ǖ念愔欣^承或?qū)崿F(xiàn)對(duì)應(yīng)的類或接口來獲取spring 的ApplicationContext對(duì)象,可是在使用是一定要注意實(shí)現(xiàn)了這些類或接口的普通java類一定要在Spring 的配置文件applicationContext.xml文件里進(jìn)行配置。否則獲取的ApplicationContext對(duì)象將為null。

方法六:通過Spring提供的ContextLoader

WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
wac.getBean(beanID);

最后提供一種不依賴于servlet,不須要注入的方式??墒琼氁⒁庖稽c(diǎn),在server啟動(dòng)時(shí)。Spring容器初始化時(shí),不能通過下面方法獲取Spring 容器,細(xì)節(jié)能夠查看spring源代碼org.springframework.web.context.ContextLoader。

JUNIT測(cè)試時(shí)ContextLoader.getCurrentWebApplicationContext()=NULL

在JUNIT測(cè)試方法中加入以下代碼,正式環(huán)境不用

MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/applicationContext.xml");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
listener.contextInitialized(event);

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論