http://blog.csdn.net/hz_blog/article/details/7688921
我們要自動裝配ApplicationContext配置信息時候,首先在web.xml配置ContextLoaderListener,下面是部分源代碼:
- public class ContextLoaderListener implements ServletContextListener {
- private ContextLoader contextLoader;
- /**
- * Initialize the root web application context.
- */
- public void contextInitialized(ServletContextEvent event) {
- this.contextLoader = createContextLoader();
- this.contextLoader.initWebApplicationContext(event.getServletContext());//注意此處
- }
- protected ContextLoader createContextLoader() {
- return new ContextLoader();
- }
- public ContextLoader getContextLoader() {
- return this.contextLoader;
- }
- public void contextDestroyed(ServletContextEvent event) {
- if (this.contextLoader != null) {
- this.contextLoader.closeWebApplicationContext(event.getServletContext());
- }
- }
- }
- public WebApplicationContext initWebApplicationContext(ServletContext servletContext)
- throws IllegalStateException, BeansException {
- if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
- throw new IllegalStateException(
- "Cannot initialize context because there is already a root application context present - " +
- "check whether you have multiple ContextLoader* definitions in your web.xml!");
- }
- servletContext.log("Initializing Spring root WebApplicationContext");
- if (logger.isInfoEnabled()) {
- logger.info("Root WebApplicationContext: initialization started");
- }
- long startTime = System.currentTimeMillis();
- try {
- // Determine parent for root web application context, if any.
- ApplicationContext parent = loadParentContext(servletContext);
- // Store context in local instance variable, to guarantee that
- // it is available on ServletContext shutdown.
- this.context = createWebApplicationContext(servletContext, parent);
- servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);//注意此處
- currentContextPerThread.put(Thread.currentThread().getContextClassLoader(), this.context);
- if (logger.isDebugEnabled()) {
- logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
- WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
- }
- if (logger.isInfoEnabled()) {
- long elapsedTime = System.currentTimeMillis() - startTime;
- logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
- }
- return this.context;
- }
- catch (RuntimeException ex) {
- logger.error("Context initialization failed", ex);
- servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
- throw ex;
- }
- catch (Error err) {
- logger.error("Context initialization failed", err);
- servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
- throw err;
- }
- }
注意注意此處部分,說明當我們要得到一個WebApplicationContext只需要在ServletContext中獲取屬性名爲WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE的常量值就可以得到一個ApplicationContext,最終可以獲取spring配置文件中任意一個Bean對象
eg:
- public String execute() throws Exception {
- // TODO Auto-generated method stub
- ApplicationContext conn=(ApplicationContext)ServletActionContext.getServletContext().getAttribute(WebApplicationContext.class.getName() + ".ROOT");
- System.out.println(conn.getBean("biz"));
- return SUCCESS;
- }
或者直接使用org.springframework.web.context.support.WebApplicationContextUtils這個抽象的工具類來獲得WebApplicationContext,該類的方法
- public static WebApplicationContext getWebApplicationContext(ServletContext sc) {
- return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
- }
還有更多方法就不多介紹了,自行查看這個類的源碼。