Spring 容器中獲取 Bean 實(shí)例的主要方式
一. 使用BeanFactory直接獲取(不推薦)
使用BeanFactory從工廠中直接獲取Bean實(shí)例,但是XmlBeanFactory類已經(jīng)廢棄,因此不建議使用,測(cè)試代碼如下:
/**
* 方式一:XmlBeanFactory已經(jīng)廢棄不建議使用
*/
@Test
public void getBeanTest1() {
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
UserInfo userInfo = (UserInfo) beanFactory.getBean("userInfo");
System.out.println(userInfo);
}
二. 在初始化時(shí)保存ApplicationContext對(duì)象
可以在初始化的時(shí)候保存ApplicationContext對(duì)象,然后通過(guò)這個(gè)對(duì)象獲取Bean,測(cè)試代碼如下:
/**
* 方式二:使用ClassPathXmlApplicationContext獲取ApplicationContext
*/
@Test
public void getBeanTest2() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserInfo userInfo = (UserInfo) applicationContext.getBean("userInfo");
System.out.println(userInfo);
}
三. 繼承自抽象類ApplicationObjectSupport
可以繼承抽象類ApplicationObjectSupport并將自己繼承的類注入到Spring容器中,示例代碼如下:
/**
* 方法三:繼承ApplicationObjectSupport來(lái)獲取ApplicationContext,
* 注意:需要把自己繼承的類注入到Spring
*/
@Test
public void getBeanTest3() {
ApplicationContextUtil2 applicationContextUtil2 = (ApplicationContextUtil2) ApplicationContextUtil.getBean("applicationContextUtil2");
UserInfo userInfo = (UserInfo) applicationContextUtil2.getBean("userInfo");
System.out.println(userInfo);
}
其中ApplicationContextUtil2的代碼如下所示:
public class ApplicationContextUtil2 extends ApplicationObjectSupport {
/**
* 通過(guò)bean的id獲取bean對(duì)象
* @param beanName
* @return
*/
public Object getBean(String beanName){
return super.getApplicationContext().getBean(beanName);
}
}
最后莫忘了將Bean注入到Spring容器中,通過(guò)注解,或者配置均可,本示例通過(guò)配置實(shí)現(xiàn)
<!-- 測(cè)試獲取bean的方式,繼承ApplicationObjectSupport需要先注入這個(gè)類 -->
<bean id="applicationContextUtil2" class="com.leo.util.ApplicationContextUtil2"/>
四. 繼承自抽象類WebApplicationObjectSupport
可以繼承抽象類WebApplicationObjectSupport并將自己繼承的類注入到Spring容器中,示例代碼如下:
/**
* 方法四:繼承WebApplicationObjectSupport來(lái)獲取ApplicationContext,
* 注意:需要把自己繼承的類注入到Spring,同時(shí)需要添加@WebAppConfiguration注解,否則會(huì)找不到web容器
*/
@Test
public void getBeanTest4() {
ApplicationContextUtil3 applicationContextUtil3 = (ApplicationContextUtil3) ApplicationContextUtil.getBean("applicationContextUtil3");
UserInfo userInfo = (UserInfo) applicationContextUtil3.getBean("userInfo");
System.out.println(userInfo);
}
其中ApplicationContextUtil3 示例代碼如下:
public class ApplicationContextUtil3 extends WebApplicationObjectSupport{
/**
* 通過(guò)bean的id獲取bean對(duì)象
* @param beanName
* @return
*/
public Object getBean(String beanName){
return super.getWebApplicationContext().getBean(beanName);
}
}
最后莫忘了將Bean注入到Spring容器中,通過(guò)注解,或者配置均可,本示例通過(guò)配置實(shí)現(xiàn)
<!-- 測(cè)試獲取bean的方式,繼承WebApplicationObjectSupport需要先注入這個(gè)類 -->
<bean id="applicationContextUtil3" class="com.leo.util.ApplicationContextUtil3"/>
五. 使用Spring提供的工具類WebApplicationContextUtils
使用Spring提供的工具類WebApplicationContextUtils來(lái)獲取WebApplicationContext對(duì)象,這個(gè)方法很常見(jiàn)于SpringMVC構(gòu)建的web項(xiàng)目中,測(cè)試代碼如下所示:
/**
* 方法五:使用WebApplicationContextUtils提供的方法獲取ApplicationContext對(duì)象
*/
@Test
public void getBeanTest5(){
//模擬ServletContext上下文,不然會(huì)出現(xià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);
//使用WebApplicationContextUtils的getRequiredWebApplicationContext方法
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
UserInfo userInfo = (UserInfo) webApplicationContext.getBean("userInfo");
System.out.println(userInfo);
//使用WebApplicationContextUtils的getWebApplicationContext方法
WebApplicationContext webApplicationContext2 = WebApplicationContextUtils.getWebApplicationContext(sc);
UserInfo userInfo2 = (UserInfo) webApplicationContext2.getBean("userInfo");
System.out.println(userInfo2);
}
六. 實(shí)現(xiàn)ApplicationContextAware接口
通過(guò)實(shí)現(xiàn)ApplicationContextAware接口,在Spring容器啟動(dòng)的時(shí)候?qū)pplicationContext注入進(jìn)去,從而獲取ApplicationContext對(duì)象,這種方法也是常見(jiàn)的獲取Bean的一種方式,測(cè)試代碼如下:
/**
*方法六:實(shí)現(xiàn)ApplicationContextAware接口獲取ApplicationContext
*/
@Test
public void getBeanTest6(){
UserInfo userInfo2 = (UserInfo) ApplicationContextUtil.getBean("userInfo");
System.out.println(userInfo2);
}
其中ApplicationContextUtil的實(shí)現(xiàn)如下:
public class ApplicationContextUtil implements ApplicationContextAware{
private static ApplicationContext applicationContext;
/**
* 通過(guò)bean的id獲取bean對(duì)象
* @param beanName
* @return
*/
public static Object getBean(String beanName){
return applicationContext.getBean(beanName);
}
/**
* 根據(jù)bean的id和類型獲取bean對(duì)象
* @param beanName
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String beanName,Class<T> clazz){
return clazz.cast(getBean(beanName));
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
七. 使用ContextLoader提供的getCurrentWebApplicationContext方法
使用ContextLoader提供的getCurrentWebApplicationContext方法提供的方法也是常用的獲取WebApplicationContext的一種方法,這個(gè)方法常見(jiàn)于SpringMVC實(shí)現(xiàn)的web項(xiàng)目中。
測(cè)試代碼如下:
/*
* 方法七:使用ContextLoader的getCurrentWebApplicationContext方法獲取WebApplicationContext
*/
@Test
public void getBeanTest7() {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/applicationContext.xml");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
listener.contextInitialized(event);
//如果不加上面的模擬創(chuàng)建ServletContext對(duì)象,會(huì)報(bào)空指針異常
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
UserInfo userInfo = (UserInfo) wac.getBean("userInfo");
System.out.println(userInfo);
}
source: cwhello.com/48993.html
記得點(diǎn)「贊」和「在看」↓
愛(ài)你們
