標籤:bst new oca current close mon startup 使用 file
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。
Spring初始化ApplicationContext為null