1. ApplicationContextAware初始化
通過它Spring容器會自動把上下文環境對象調用ApplicationContextAware接口中的setApplicationContext方法。
我們在ApplicationContextAware的實現類中,就可以通過這個上下文環境對象得到Spring容器中的Bean。
使用方法如下:
1.實現ApplicationContextAware接口:
package com.bis.majian.practice.module.spring.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class SpringContextHelper implements ApplicationContextAware { private static ApplicationContext context = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } public static Object getBean(String name){ return context.getBean(name); } }
2.在Spring的配置文件中配置這個類,Spring容器會在加載完Spring容器后把上下文對象調用這個對象中的setApplicationContext方法:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName"> <bean id="springContextHelper" class="com.bis.majian.practice.module.spring.util.SpringContextHelper"></bean> <context:component-scan base-package="com.bis.majian.practice.module.*" /> </beans>
3.在web項目中的web.xml中配置加載Spring容器的Listener:
<!-- 初始化Spring容器,讓Spring容器隨Web應用的啟動而自動啟動 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
4.在項目中即可通過這個SpringContextHelper調用getBean()方法得到Spring容器中的對象了。
2. ApplicationContext加載機制
1.加載器目前有兩種選擇:ContextLoaderListener和ContextLoaderServlet
這兩者在功能上完全等同,只是一個是基於Servlet2.3版本中新引入的Listener接口實現 而另一個基於Servlet接口實現。開發中可根據目標Web容器的實際情況進行選擇。 配置非常簡單,在web.xml中增加:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
或者
<servlet> <servlet-name>context</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet>
通過以上配置,Web容器會自動加載/WEB-INF/applicationContext.xml初始化
ApplicationContext實例,如果需要指定配置文件位置,可通過context-param加以指定:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-bean.xml,classpath:spring-jamon.xml </param-value> </context-param>
2.Spring提供ApplicationContext多種實現機制
簡單的用ApplicationContext做測試的話,獲得Spring中定義的Bean實例(對象).可以用:
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); RegisterDAO registerDAO = (RegisterDAO)ac.getBean("RegisterDAO"); 如果是兩個以上: ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","dao.xml"}); 或者用通配符: ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml");
spring為ApplicationContext提供的3種實現分別為:
ClassPathXmlApplicationContext,FileSystemXmlApplicationContext和XmlWebApplicationContext,其中XmlWebApplicationContext是專為Web工程定制的。
其中XmlWebApplicationContext是專為Web工程定制的。使用舉例如下:
(1)FileSystemXmlApplicationContext
//加載單個配置文件 ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml"); String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"}; //加載多個配置文件 ApplicationContext ctx = new FileSystemXmlApplicationContext(locations ); //根據具體路徑加載文件 ApplicationContext ctx =new FileSystemXmlApplicationContext("D:/project/bean.xml");
注: 這種方式適用於采用Spring框架的獨立應用程序,需要程序通過配置文件手工初始化Spring的情況。
(2)ClassPathXmlApplicationContext
//加載單個配置文件 ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); //配置完成之后,即可通過ContextLoader工具類獲取WebApplicationContext WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); LoginAction action=(LoginAction) wac.getBean("action");
(3) XmlWebApplicationContext
ServletContext servletContext = request.getSession().getServletContext(); ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
注:這種方式適合於采用Spring框架的B/S系統,通過ServletContext對象獲取ApplicationContext對象,然后在通過它獲取需要的類實例。
3.如何獲取ApplicationContext
(1)繼承自抽象類ApplicationObjectSupport
說明:抽象類ApplicationObjectSupport提供getApplicationContext()方法,可以方便的獲取到ApplicationContext。
Spring初始化時,會通過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象注入。
(2)繼承自抽象類WebApplicationObjectSupport
說明:類似上面方法,調用getWebApplicationContext()獲取WebApplicationContext (3)實現接口ApplicationContextAware
說明:實現該接口的setApplicationContext(ApplicationContext context)方法,並保存 ApplicationContext 對象。
Spring初始化時,會通過該方法將ApplicationContext對象注入。
實現方法:
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
applicationContext = arg0; } 獲取bean: ITaskService bean = (ITaskService)applicationContext.getBean(taskServiceName);
3. Spring獲取WebApplicationContext為null解決方案
在web.xml中配置Spring,配置如下
<servlet> <servlet-name>springServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
在Servlet中通過WebApplicationContextUtils.getWebApplicationContext(getServletContext())獲取WebApplicationContext對象為null。這是由於除了配置DispatcherServlet,還需要配置ContextLoaderServlet,否則無法獲取WebApplicationContext。配置方法如下,在web.xml中加入
<servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet>
案例2:
查找原因:xml文件的加載順序不正確,ServiceBeanUtils還沒有加載, ApplicationContext還沒有初始化,而服務啟動時就有個類通過調用ApplicationContext去取bean進行初始化了
解決方案:先加載ServiceBeanUtils類,去初始化ApplicationContext,然后再加載要調用ApplicationContext的類去初始化此類
4.ApplicationContext初始化方式
1. 在獨立應用程序中,獲取ApplicationContext:
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.close();//釋放資源
2. 在web環境中,獲取ApplicationContext:
A)ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
B)String contextpath = "org.springframework.web.context.WebApplicationContext.ROOT";
WebApplicationContext context = request.getSession().getServletContext().getAttribute(contextpath);
5.常用獲取spring 中bean的方式總結
方法一:在初始化時保存ApplicationContext對象
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId");
說明:這種方式適用於采用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對象,然后在通過它獲取需要的類實例。
方法五:實現接口ApplicationContextAware
說明:實現該接口的setApplicationContext(ApplicationContext context)方法,並保存ApplicationContext 對象。
Spring初始化時,會通過該方法將ApplicationContext對象注入。
使用的時候一定要注意實現了這些類或接口的普通java類一定要在Spring 的配置文件application-context.xml文件中進行配置。否則獲取的ApplicationContext對象將為null。