java中,常见的 Context 有很多, 例如: ServletContext, ActionContext, ServletActionContext, ApplicationContext, PageContext, SessionContext…
熟悉spring是怎样在web容器中启动起来的。spring的启动过程其实就是其IoC容器的启动过程,对于web程序,IoC容器启动过程即是建立上下文的过程。
servletContext 是web应用程序的大环境,用于存储整个web应用程序级别的对象
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ClassName clazz =(ClassName)ctx.getBean("beanName");
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
ClassName clazz =(ClassName)ctx.getBean("beanName");
HttpSession session = request.getSession();
ServletContext context = session.getServletContext(); //arg0.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
ClassName clazz = (ClassName)ctx.getBean("beanName");
import org.springframework.context.support.ApplicationObjectSupport;
public class ContextOne extends ApplicationObjectSupport
{
......
}
........
ContextOne one = new ContextOne();
one.getApplicationContext();
import org.springframework.web.context.support.WebApplicationObjectSupport;
public class ContextOne extends WebApplicationObjectSupport {
.......
}
........
ContextOne one = new ContextOne();
one.getApplicationContext();
-方法六:实现接口ApplicationContextAware当一个类实现了ApplicationContextAware接口后,这个类就可以获得Spring配置文件中的所引用到的bean对象。说明:实现该接口的setApplicationContext(ApplicationContextcontext)方法,并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext对象注入。
http://blog.csdn.net/kaiwii/article/details/6872642
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
//通过接口ApplicationContextAware获得spring上下文
public class Context implements ApplicationContextAware {
private static ApplicationContext ctx;
//设置ApplicationContext对象
public void setApplicationContext(ApplicationContext context)
throws BeansException {
// TODO Auto-generated method stub
ctx=context;
}
//通过beanName获得实例
public static Object getBean(String beanName)
{
return ctx.getBean(beanName);
}
}
一个 WEB 运用程序只有一个 ServletContext 实例, 它是在容器(包括 JBoss, Tomcat 等)完全启动 WEB 项目之前被创建, 生命周期伴随整个 WEB 运用。
当在编写一个 Servlet 类的时候, 首先是要去继承一个抽象类 HttpServlet, 然后可以直接通过 getServletContext() 方法来获得 ServletContext 对象。
这是因为 HttpServlet 类中实现了 ServletConfig 接口, 而 ServletConfig 接口中维护了一个 ServletContext 的对象的引用。
利用 ServletContext 能够获得 WEB 运用的配置信息, 实现在多个 Servlet 之间共享数据等。
url
jdbc:oracle:thin:@localhost:1521:ORC
username
scott
password
tigger654321
ConnectionServlet
net.yeah.fancydeepin.servlet.ConnectionServlet
ConnectionServlet
/ConnectionServlet.action
PrepareConnectionServlet
net.yeah.fancydeepin.servlet.PrepareConnectionServlet
PrepareConnectionServlet
/PrepareConnectionServlet.action
package net.yeah.fancydeepin.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PrepareConnectionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException {
ServletContext context = getServletContext();
String url = context.getInitParameter("url");
String username = context.getInitParameter("username");
String password = context.getInitParameter("password");
context.setAttribute("url", url);
context.setAttribute("username", username);
context.setAttribute("password", password);
}
protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("ConnectionServlet.action");
}
}
package net.yeah.fancydeepin.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
public class ConnectionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println("***************************************");
System.out.println("URL: " + context.getAttribute("url"));
System.out.println("Username: " + context.getAttribute("username"));
System.out.println("Password: " + context.getAttribute("password"));
System.out.println("***************************************");
super.service(request, response);
}
}
当访问 PrepareConnectionServlet.action 时, 后台打印输出:
***********************************************
URL: jdbc:oracle:thin:@localhost:1521:ORC
Username: scott
Password: 654321
***********************************************
ActionContext 是当前 Action 执行时的上下文环境, ActionContext 中维护了一些与当前 Action 相关的对象的引用, 如: Parameters (参数), Session (会话), ValueStack (值栈), Locale (本地化信息) 等。
在 Struts1 时期, Struts1 的 Action 与 Servlet API 和 JSP 技术的耦合度都很紧密, 属于一个侵入式框架:
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
// TODO Auto-generated method stub
return null;
}
到了 Struts2 时期, Struts2 的体系结构与 Struts1 之间存在很大的不同。Struts2 在 Struts1 的基础上与 WebWork 进行了整合, 成为了一个全新的框架。
在 Struts2 里面, 则是通过 WebWork 来将与 Servlet 相关的数据信息转换成了与 Servlet API 无关的对象, 即 ActionContext 对象。
这样就使得了业务逻辑控制器能够与 Servlet API 分离开来。另外, 由于 Struts2 的 Action 是每一次用户请求都产生一个新的实例, 因此, ActionContext 不存在线程安全问题, 可以放心使用。
package net.yeah.fancydeepin.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
public class ContextAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private String password;
public String execute(){
ActionContext context = ActionContext.getContext();
ValueStack value = context.getValueStack();
value.set("username", username);
value.set("password", password);
Map session = context.getSession();
session.put("url", "http://www.blogjava.net/fancydeepin");
return SUCCESS;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
}
当访问 context.action 并传给相应的参数的时候, 在浏览器中会输出相应的信息。
留意到上面 Struts2 的 Action 中并有没添加属性的 getting 方法, 而是手动的将参数值放到值栈(ValueStack)中的, 否则页面是得不到参数来输出的。
首先, ServletActionContext 是 ActionContext 的一个子类。ServletActionContext 从名字上来看, 意味着它与 Servlet API 紧密耦合。
ServletActionContext 的构造子是私有的, 主要是提供了一些静态的方法, 可以用来获取: ActionContext, ActionMapping, PageContext,
HttpServletRequest, HttpServletResponse, ServletContext, ValueStack, HttpSession 对象的引用。
public String execute(){
//或 implements ServletRequestAware
HttpServletRequest request = ServletActionContext.getRequest();
//或 implements ServletResponseAware
HttpServletResponse response = ServletActionContext.getResponse();
//或 implements SessionAware
HttpSession session = request.getSession();
//或 implements ServletContextAware
ServletContext context = ServletActionContext.getServletContext();
return SUCCESS;
}
DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring IoC容器无缝集成,从而可以获得Spring的所有好处。
DispatcherServlet主要用作职责调度工作,本身主要用于控制流程,主要职责如下:
从以上我们可以看出DispatcherServlet主要负责流程的控制(而且在流程中的每个关键点都是很容易扩展的)。
chapter2
org.springframework.web.servlet.DispatcherServlet
1
chapter2
/
还可以使用初始化参数的构造配置:
chapter2
org.springframework.web.servlet.DispatcherServlet
1
contextConfigLocation
classpath:spring-servlet-config.xml
如果使用如上配置,Spring Web MVC框架将加载“classpath:spring-servlet-config.xml”来进行初始化上下文而不是“/WEB-INF/[servlet名字]-servlet.xml”。
集成Web环境的通用配置:
contextConfigLocation
classpath:spring-common-config.xml,
classpath:spring-budget-config.xml
org.springframework.web.context.ContextLoaderListener
如上配置是Spring集成Web环境的通用配置;一般用于加载除Web层的Bean(如DAO、Service等),以便于与其他任何Web框架集成。
contextConfigLocation:表示用于加载Bean的配置文件;
contextClass:表示用于加载Bean的ApplicationContext实现类,默认WebApplicationContext。
创建完毕后会将该上下文放在ServletContext:
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,this.context);
ContextLoaderListener
初始化的上下文和DispatcherServlet初始化的上下文关系如下图:
ContextLoaderListener初始化的上下文加载的Bean是对于整个应用程序共享的,不管是使用什么表现层技术,一般如DAO层、Service层Bean;
DispatcherServlet初始化的上下文加载的Bean是只对Spring Web MVC有效的Bean,如Controller、HandlerMapping、HandlerAdapter等等,该初始化上下文应该只加载Web相关组件。
DispatcherServlet初始化的过程主要做了两件事情:
spring的启动是建筑在servlet容器之上的,所有web工程的初始位置就是web.xml,它配置了servlet的上下文(context)和监听器(Listener),下面就来看看web.xml里面的配置:
ServletContextListener
com.trace.app.framework.listeners.ApplicationListener
service_dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/spring/services/service_dispatcher-servlet.xml
1
contextConfigLocation
/WEB-INF/spring/application_context.xml
org.springframework.web.context.ContextLoaderListener
com.trace.app.framework.listeners.MySessionListener
contextConfigLocation
/WEB-INF/spring/application_context.xml
org.springframework.web.context.ContextLoaderListener
spring的启动其实就是IOC容器的启动过程,通过上述的第一段配置是初始化上下文,然后通过后一段的的来加载配置文件,其中调用的spring包中的ContextLoaderListener这个上下文监听器,ContextLoaderListener是一个实现了ServletContextListener接口的监听器,他的父类是 ContextLoader,在启动项目时会触发contextInitialized上下文初始化方法。下面我们来看看这个方法:
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}
可以看到,这里是调用了父类ContextLoader的initWebApplicationContext(event.getServletContext());方法,很显然,这是对ApplicationContext的初始化方法,也就是到这里正是进入了springIOC的初始化。
接下来再来看看initWebApplicationContext又做了什么工作,先看看代码:
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!");
}
Log logger = LogFactory.getLog(ContextLoader.class);
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();
try {
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent ->
// determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
cwac.setParent(parent);
}
configureAndRefreshWebApplicationContext(cwac, servletContext);
}
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = this.context;
}
else if (ccl != null) {
currentContextPerThread.put(ccl, 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;
}
这个方法还是有点长的,其实仔细看看,除去异常错误处理,这个方法主要做了三件事:
上述代码中createWebApplicationContext(servletContext)方法即是完成创建WebApplicationContext工作,也就是说这个方法创建了上下文对象,支持用户自定义上下文对象,但必须继承ConfigurableWebApplicationContext,而Spring MVC默认使用ConfigurableWebApplicationContext作为ApplicationContext(它仅仅是一个接口)的实现。
再往下走,有一个方法configureAndRefreshWebApplicationContext就是用来加载spring配置文件中的Bean实例的。这个方法于封装ApplicationContext数据并且初始化所有相关Bean对象。它会从web.xml中读取名为 contextConfigLocation的配置,这就是spring xml数据源设置,然后放到ApplicationContext中,最后调用传说中的refresh方法执行所有Java对象的创建。
最后完成ApplicationContext创建之后就是将其放入ServletContext中,注意它存储的key值常量。
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
web.xml的相关配置:
service_dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/spring/services/service_dispatcher-servlet.xml
1
这里采用这种自定义初始化参数的配置方式,当然也可以使用默认的。这里Spring Web MVC框架将加载“classpath:service_dispatcher-servlet.xml”来进行初始化上下文而不是“/WEB-INF/[servlet名字]-servlet.xml”。
通过上述配置文件很明显可以看出,springMVC的起始位置是DispatcherServlet(还是spring提供的):
public class DispatcherServlet extends FrameworkServlet {
... ...
}
这个类的父类是FrameworkServlet,FrameworkServlet又继承了HttpServletBean类,HttpServletBean又继承了HttpServlet,HttpServlet继承了GenericServlet
public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {
... ...
}
public abstract class HttpServletBean extends HttpServlet
implements EnvironmentCapable, EnvironmentAware {
... ...
}
public abstract class HttpServlet extends GenericServlet
implements java.io.Serializable
{
... ...
}
所以在这样一个web容器启动的时候会调用HttpServletBean的init方法,这个方法覆盖了GenericServlet中的init方法。让我我们来看看代码:
@Override
public final void init() throws ServletException {
if (logger.isDebugEnabled()) {
logger.debug("Initializing servlet '" + getServletName() + "'");
}
// Set bean properties from init parameters.
try {
PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
initBeanWrapper(bw);
bw.setPropertyValues(pvs, true);
}
catch (BeansException ex) {
logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
throw ex;
}
// Let subclasses do whatever initialization they like.
initServletBean();
if (logger.isDebugEnabled()) {
logger.debug("Servlet '" + getServletName() + "' configured successfully");
}
}
该初始化方法的主要作用:将Servlet初始化参数(init-param)设置到该组件上(如contextAttribute、contextClass、namespace、contextConfigLocation),通过BeanWrapper简化设值过程,方便后续使用;提供给子类初始化扩展点,initServletBean(),该方法由FrameworkServlet覆盖。
FrameworkServlet继承HttpServletBean,通过initServletBean()进行Web上下文初始化,该方法主要覆盖一下两件事情:初始化web上下文;提供给子类初始化扩展点。
@Override
protected final void initServletBean() throws ServletException {
getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
if (this.logger.isInfoEnabled()) {
this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
}
long startTime = System.currentTimeMillis();
try {
this.webApplicationContext = initWebApplicationContext();
initFrameworkServlet();
}
catch (ServletException ex) {
this.logger.error("Context initialization failed", ex);
throw ex;
}
catch (RuntimeException ex) {
this.logger.error("Context initialization failed", ex);
throw ex;
}
if (this.logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
this.logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +
elapsedTime + " ms");
}
}
DispatcherServlet继承FrameworkServlet,并实现了onRefresh()方法提供一些前端控制器相关的配置。
整个DispatcherServlet初始化的过程和做了些什么事情,具体主要做了如下两件事情:
onRefresh方法代码如下:
@Override
protected void onRefresh(ApplicationContext context) {
initStrategies(context);
}
/**
* Initialize the strategy objects that this servlet uses.
* May be overridden in subclasses in order to initialize further strategy objects.
*/
protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}