关于spring获取webApplication.getBean多种途径和简单解释

ApplicationContext ac1 = new FileSystemXmlApplicationContext("com/spark/system/applicationContext.xml");//如果配置文件放在文件系统的目录下则优先使用该方式//com/spark/system/applicationContext.xml等价于"file:com/spark/system/applicationContext.xml"ac1.getBean("beanId");//ApplicationContext ac2=new ClassPathXmlApplicationContext("com/spark/system/applicationContext.xml");//如果配置文件在类路径下则优先使用该方式//com/spark/system/applicationContext.xml 等价于"classpath:com/spark/system/applicationContext.xml" ac2.getBean("beanId");说明:

这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。

public void getBean(HttpServletRequest req,HttpSession se){//se.getServletContext() 也可以WebApplicationContext wac=(WebApplicationContext)req.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);wac.getBean("");}说明:此种方式正是我们下面所提到的WebApplicationContextUtils 工具类中getWebApplicationContext(ServletContext sc) 方法的内部实现,,以下方式是通过spring 提供的WebApplicationContextUtils 工具类获取WebApplicationContext

方式一:

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。

方式二:

import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationObjectSupport;public class ApplicationContextUtils extends WebApplicationObjectSupport{public WebApplicationContext isgetWebApplicationContext(){return super.getWebApplicationContext();}}

继承自抽象类WebApplicationObjectSupport说明:抽象类WebApplicationObjectSupport 继承自ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。Spring初始化时,会通过该ApplicationObjectSupport 的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。当然直接继承ApplicationObjectSupport自己实现也可以,既然spring 提供了更方便的抽象工具类WebApplicationObjectSupport 建议使用它。以免出现问题

下面看WebApplicationObjectSupport关键源码(红色部分)

/*** Eclipse Class Decompiler, copyright (c) 2012 cnfree (cnfree2000@hotmail.com) ***/package org.springframework.web.context.support;import java.io.File;import javax.servlet.ServletContext;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ApplicationObjectSupport;import org.springframework.web.context.ServletContextAware;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.util.WebUtils;public abstract class WebApplicationObjectSupport <span style="color:#FF0000;">extends ApplicationObjectSupport</span> implements ServletContextAware{private ServletContext servletContext;public final void setServletContext(ServletContext servletContext){if (servletContext != this.servletContext){this.servletContext = servletContext;if (servletContext != null) initServletContext(servletContext);}}protected boolean isContextRequired(){return true;}protected void initApplicationContext(ApplicationContext context){super.initApplicationContext(context);if ((this.servletContext == null)&& (context instanceof WebApplicationContext)){this.servletContext = ((WebApplicationContext)context).getServletContext();if (this.servletContext != null)initServletContext(this.servletContext);}}protected void initServletContext(ServletContext servletContext){}<span style="color:#FF0000;">protected final WebApplicationContext getWebApplicationContext()throws IllegalStateException{ApplicationContext ctx = getApplicationContext();if (ctx instanceof WebApplicationContext){ return ((WebApplicationContext)getApplicationContext()); }if (isContextRequired()){ throw new IllegalStateException("WebApplicationObjectSupport instance [" + this+ "] does not run in a WebApplicationContext but in: "+ ctx); }return null;}</span>protected final ServletContext getServletContext()throws IllegalStateException{if (this.servletContext != null){ return this.servletContext; }ServletContext servletContext = getWebApplicationContext().getServletContext();if ((servletContext == null) && (isContextRequired())){ throw new IllegalStateException("WebApplicationObjectSupport instance ["+ this+ "] does not run within a ServletContext. Make sure the object is fully configured!"); }return servletContext;}protected final File getTempDir() throws IllegalStateException{return WebUtils.getTempDir(getServletContext());}}下面是ApplicationObjectSupport源码

并且为之实践了关怀和付出的善举。对于我性情中的易感和怨薄,

关于spring获取webApplication.getBean多种途径和简单解释

相关文章:

你感兴趣的文章:

标签云: