方式一:java
public class SpringUtil { public static ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); public static Object getBean(String serviceName){ return context.getBean(serviceName); } }
使用實例:UserDao userDao = (UserDao)SpringUtil.getBean("userDao");web
方式二:spring
@Component public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; // Spring應用上下文環境 /* * 實現了ApplicationContextAware 接口,必須實現該方法; *經過傳遞applicationContext參數初始化成員變量applicationContext */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtil.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { return applicationContext; } @SuppressWarnings("unchecked") public static <T> T getBean(String name) throws BeansException { return (T) applicationContext.getBean(name); } }
注意:這個地方使用了Spring的註解@Component,若是不是使用annotation的方式,而是使用xml的方式管理Bean,記得寫入配置文件:tomcat
<bean id="springContextUtil" class="com.xxx.util.SpringContextUtil" singleton="true" />
方式三:服務器
ClassPathXmlApplicationContext resource = new ClassPathXmlApplicationContext(new String[]{ "applicationContext-ibatis-oracle.xml", "applicationContext.xml", "applicationContext-data-oracle.xml" } ); BeanFactory factory = resource; UserDao userDao = (UserDao) factory.getBean("userDao");
方式四: 利用ClassPathResource
能夠從classpath中讀取XML文件oracle
Resource cr = new ClassPathResource("applicationContext.xml"); BeanFactory bf=new XmlBeanFactory(cr); UserDao userDao = (UserDao)bf.getBean("userDao");
加載一個xml文件org.springframework.beans.factory.config.PropertyPlaceholderConfigurer不起做用
方式五:利用XmlWebApplicationContext讀取app
XmlWebApplicationContext ctx = new XmlWebApplicationContext(); ctx.setConfigLocations(new String[] {"/WEB-INF/ applicationContext.xml"); ctx.setServletContext(pageContext.getServletContext()); ctx.refresh(); UserDao userDao = (UserDao ) ctx.getBean("userDao ");
方式六:利用FileSystemResource讀取框架
Resource rs = new FileSystemResource("D:/tomcat/webapps/test/WEB-INF/classes/ applicationContext.xml"); BeanFactory factory = new XmlBeanFactory(rs); UserDao userDao = (UserDao )factory.getBean("userDao");
值得注意的是:利用FileSystemResource,則配置文件必須放在project直接目錄下,或者寫明絕對路徑,不然就會拋出找不到文件的異常。
方式七:利用FileSystemXmlApplicationContext讀取
能夠指定XML定義文件的相對路徑或者絕對路徑來讀取定義文件。
方法一:webapp
String[] path={ "WebRoot/WEB-INF/applicationContext.xml", "WebRoot/WEB-INF/applicationContext_task.xml" }; ApplicationContext context = new FileSystemXmlApplicationContext(path);
方法二:ide
String path="WebRoot/WEB-INF/applicationContext*.xml"; ApplicationContext context = new FileSystemXmlApplicationContext(path);
方法三:
ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:地址");
沒有classpath的話就是從當前的工做目錄
方式八:經過Spring提供的工具類獲取ApplicationContext對象
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對象注入。
如下是實現ApplicationContextAware接口方式的代碼,前面兩種方法相似:
public class SpringContextUtil implements ApplicationContextAware { // Spring應用上下文環境 private static ApplicationContext applicationContext; /** * 實現ApplicationContextAware接口的回調方法,設置上下文環境 * * @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提供的後三種方法能夠實如今普通的類中繼承或實現相應的類或接口來獲取spring 的ApplicationContext對象,可是在使用是必定要注意實現了這些類或接口的普通java類必定要在Spring 的配置文件applicationContext.xml文件中進行配置。不然獲取的ApplicationContext對象將爲null。
方式十二:經過Spring提供的ContextLoader
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); wac.getBean(beanID);
最後提供一種不依賴於servlet,不須要注入的方式。可是須要注意一點,在服務器啓動時,Spring容器初始化時,不能經過如下方法獲取Spring 容器,細節能夠查看spring源碼org.springframework.web.context.ContextLoader。