国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Spring在代碼中如何獲取bean

發(fā)布時(shí)間:2021-09-27 17:50 來(lái)源:億速云 閱讀:0 作者:小新 欄目: 開(kāi)發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)Spring在代碼中如何獲取bean的內容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

方法如下

方法一:通過(guò)讀取XML文件反射生成對象  方法二:通過(guò)Spring提供的utils類(lèi)獲取ApplicationContext對象  方法三:繼承自抽象類(lèi)ApplicationObjectSupport  方法四:繼承自抽象類(lèi)WebApplicationObjectSupport  方法五:實(shí)現接口A(yíng)pplicationContextAware  方法六:通過(guò)Spring提供的ContextLoader

獲取spring中bean的方式總結:

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

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

說(shuō)明:這樣的方式適用于採用Spring框架的獨立應用程序,須要程序通過(guò)配置文件手工初始化Spring的情況。

方法二:通過(guò)Spring提供的工具類(lèi)獲取ApplicationContext對象

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

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

方法三:繼承自抽象類(lèi)ApplicationObjectSupport

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

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

方法四:繼承自抽象類(lèi)WebApplicationObjectSupport

說(shuō)明:類(lèi)似上面方法。調用getWebApplicationContext()獲取WebApplicationContext

方法五:實(shí)現接口A(yíng)pplicationContextAware

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

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

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

盡管,spring提供的后三種方法能夠實(shí)如今普通的類(lèi)中繼承或實(shí)現對應的類(lèi)或接口來(lái)獲取spring 的ApplicationContext對象,可是在使用是一定要注意實(shí)現了這些類(lèi)或接口的普通java類(lèi)一定要在Spring 的配置文件applicationContext.xml文件里進(jìn)行配置。否則獲取的ApplicationContext對象將為null。

方法六:通過(guò)Spring提供的ContextLoader

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

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

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

在JUNIT測試方法中加入以下代碼,正式環(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);

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

99久久无色码中文字幕人妻| 久草热久草热线频97精品| 亚洲午夜无码久久久久小说| 亚洲成AV人片在线观看无APP| 女人与公拘交酡全过女免费| 亚洲熟妇少妇任你躁在线观看|