tomcat与springmvc 结合 之---第17篇 StandContext容器和SpringMVC的WebApplicationContext的联系

writedby 张艳涛,

上一篇分析了,dispatcherservlet通过getServletConfig 方法获取了web.xml定义的<param-init>属性的过程

那么在如果在Controller里面使用servletContext 和seervletConfig对象怎么办?

public class NoannaContoller implements Controller , EnvironmentAware, ApplicationContextAware {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
                                      HttpServletResponse response) throws {}
    private Environment environment=null;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment=environment;
    }
    private    ApplicationContext applicationContext=null;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
    }
}

答案是实现XXXAware,那么如果给红色的打个断点,那么看看,

tomcat与springmvc 结合 之---第17篇 StandContext容器和SpringMVC的WebApplicationContext的联系

可以看到是在bean对象noannaController对象初始化的时候给对象进行了赋值,

调用关系

tomcat与springmvc 结合 之---第17篇 StandContext容器和SpringMVC的WebApplicationContext的联系

tomcat与springmvc 结合 之---第17篇 StandContext容器和SpringMVC的WebApplicationContext的联系

tomcat与springmvc 结合 之---第17篇 StandContext容器和SpringMVC的WebApplicationContext的联系

tomcat与springmvc 结合 之---第17篇 StandContext容器和SpringMVC的WebApplicationContext的联系

tomcat与springmvc 结合 之---第17篇 StandContext容器和SpringMVC的WebApplicationContext的联系

tomcat与springmvc 结合 之---第17篇 StandContext容器和SpringMVC的WebApplicationContext的联系

tomcat与springmvc 结合 之---第17篇 StandContext容器和SpringMVC的WebApplicationContext的联系tomcat与springmvc 结合 之---第17篇 StandContext容器和SpringMVC的WebApplicationContext的联系tomcat与springmvc 结合 之---第17篇 StandContext容器和SpringMVC的WebApplicationContext的联系

这里就非常明确了实现aware接口的类,会在创建applicationcontext过程中将自己传给aware类

这里还设计一个问题就是这个类叫org.springframework.web.context.support.XmlWebApplicationContext,那创建之后会存到tomcat的StandardContext容器的

ApplicationContext中,存的key 是org.springframework.web.servlet.FrameworkServlet.CONTEXT.springmvc

如图

tomcat与springmvc 结合 之---第17篇 StandContext容器和SpringMVC的WebApplicationContext的联系

注:org.springframework.web.servlet.FrameworkServlet.CONTEXT.springmvc 是由

SERVLET_CONTEXT_PREFIX + getServletName();
其中org.springframework.web.servlet.FrameworkServlet.CONTEXT. + servlet的名字来确定
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器、映射器等) 如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-servlet.xml(springmvc-servlet.xml) -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>

 tomcat与springmvc 结合 之---第17篇 StandContext容器和SpringMVC的WebApplicationContext的联系