常用的5種獲取spring 中bean的方式總結:
方法一:在初始化時保存ApplicationContext對象
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:META-INF/spring/applicationContext.xml");
String[] configs = { "file:src/springConfig/springMVC.xml" };
XmlWebApplicationContext context = new XmlWebApplicationContext();
context.setConfigLocations(configs);
說明:這種方式適用於採用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對象注入。
方法六:實現接口BeanFactoryAware
說明:實現該接口的setBeanFactory(BeanFactory beanFactory)方法,並保存BeanFactory對象。Spring初始化時,會通過該方法將 BeanFactory 對象注入。
雖然,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
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
wac.getBean(beanID);
方法八:通過HttpServletRequest request對象獲取
WebApplicationContext webApplicationContent = RequestContextUtils.getWebApplicationContext(request);