JavaEE唯爱Java服务器端编程

Spring MVC中servlet架构

2017-08-18  本文已影响53人  HeartGo

IOC

控制反转,用一句话解释这个概念就是将对象的创建和获取提取到外部。由外部容器提供需要的组件。

AOP

确实就是代理模式的体现,
面向对象编程将程序分解成各个层次的对象,面向切面编程将程序运行过程分解成各个切面。
AOP从程序运行角度考虑程序的结构,提取业务处理过程的切面,oop是静态的抽象,aop是动态的抽象,
Servlet继承结构中一共有五个类,
Java:GenericServlet,HttpServlet。
Spring:HttpServletBean,FrameWorkServlet和DispatcherServlet.
spring中,这三个类实现了三个接口EnvironmentCapple,EnvironmentAware和Application-ContextAware.
xxxAware在spring中表示对xxx的感知,就是在某个类需要使用spring的一些东西,就可以通过实现xxxAware告诉spring,spring看到后就会给你送过来,接收的方式是通过实现唯一的方法set-XXX。EnvironmentCappable,就是具有Environment的能力,可以提供Environment,EnvironmentCappable唯一的方法是Environment getEnvironment(),用于实现EnvironmentCapable接口的类,告诉spring它可以提供Environment,当spring需要Environment的时候就会调用getEnvironment()方法跟他要。
Environment就是环境,和servletcontex有点类似,保存了全局的信息。
在HttpServeltBean中的Environment使用的是standard-servlet-Environment,在createEnvironment方法中创建,封装了servletContext,还封装了,servletconfig,JndiProperty(Initial Contexts设置默认属性。),系统环境变量,系统属性等。都封装到propertySources属性下。

HttpServletBean

HttpServletBean分别实现了HttpServlet,EnvironmentCapable,EnvironmentAware.
简单扩展HttpServlet,给各种类型的servlet提供了一个便利的超类,提供了对属性的操作。
在HttpServletBean的init方法中,首先将Servlet中配置的参数使用BeanWrapper设置到DispatcherServlet的相关属性。然后调用模板方法initServletBean,进行初始化。

FrameworkServlet

入口:initServletBean()
初始化webApplicationContext和FrameworkServlet.
initFrameworkServlet是模板方法,子类覆盖后可以在里面做一些初始化的工作,但子类并没有用它。
initWebApplicationContext:
1.获取spring的根容器,rootContext.
2.设置webApplicationContext并根据情况调用onRefresh方法。
3.将webApplicationContext设置到ServletContext中。
根据publishContext标志位判断是否将创建webApplicationContext设置到Servlet的属性中。
配置servlet时设置的初始化参数:
1.contextAtteibute:在ServletContext中,用作webApplicationContext的属性名称。
2.contextclass:创建webApplicationContext的类型。
3.contextConfigloaction:spring mvc配置文件的位置。
4.publicContext:是否将webApplicationContext设置到ServletContext的属性。

DispatcherServlet

onRefresh()是DispatcherServlet的入口方法,onRefresh中简单地调用了initStrategies方法,在initStrategies中调用调用了9个init方法。

上一篇下一篇

猜你喜欢

热点阅读