- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- Spring在代碼中如何獲取bean
這篇文章給大家分享的是有關(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í),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站