php建站系统,无锡企业网上办事大厅,深圳高端logo设计公司,网站建设咨询话术技巧Servlet3.0
Servlet3.0是基于注解配置的理论基础。
Servlet3.0引入了基于注解配置Servlet的规范#xff0c;提出了可拔插的ServletContext初始化方式#xff0c;引入了一个叫ServletContainerInitializer的接口。 An instance of the ServletContainerInitializer is looke…Servlet3.0
Servlet3.0是基于注解配置的理论基础。
Servlet3.0引入了基于注解配置Servlet的规范提出了可拔插的ServletContext初始化方式引入了一个叫ServletContainerInitializer的接口。 An instance of the ServletContainerInitializer is looked up via the jar services API by the container at container / application startup time. The framework providing an implementation of the ServletContainerInitializer MUST bundle in the META-INF/services directory of the jar file a file called javax.servlet.ServletContainerInitializer, as per the jar services API, that points to the implementation class of the ServletContainerInitializer. Servlet3.0规范约定WEB容器比如tomcat要通过SPI的方式检查应用jar包的META-INF/services目录下的Servlet容器的初始化类ServletContainerInitializer接口的实现类通过调用该实现类的onStartup方法完成Servlet容器的初始化。
此外Servlet3.0还引入了HandlesTypes注解用来指定Servlet容器初始化过程中WEB容器会认为应用中的哪些类由HandlesTypes指定会参与到Servlet容器的初始化过程中来。
SpringMVC正是通过以上方式实现Servlet容器的初始化的
SpringMVC Servlet容器初始化过程
按照上述理论的指引探究注解方式配置Spring MVC的原理就没那么困难了。
先看下图 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rcwAxqtb-1693146405131)(/img/bVc9kPt)] 很容易的我们发现SpringMVC指定的Servlet容器初始化的实现类为org.springframework.web.SpringServletContainerInitializer。
所以我们找到他看看
HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
}确实是ServletContainerInitializer接口的实现类HandlesTypes指定的是WebApplicationInitializer这个WebApplicationInitializer究竟是个啥东东我们先放放我们先来研究一下 SpringServletContainerInitializer类的onStartup方法。
Overridepublic void onStartup(Nullable SetClass? webAppInitializerClasses, ServletContext servletContext)throws ServletException {ListWebApplicationInitializer initializers new LinkedList();if (webAppInitializerClasses ! null) {for (Class? waiClass : webAppInitializerClasses) {// Be defensive: Some servlet containers provide us with invalid classes,// no matter what HandlesTypes says...if (!waiClass.isInterface() !Modifier.isAbstract(waiClass.getModifiers()) WebApplicationInitializer.class.isAssignableFrom(waiClass)) {try {initializers.add((WebApplicationInitializer)ReflectionUtils.accessibleConstructor(waiClass).newInstance());}catch (Throwable ex) {throw new ServletException(Failed to instantiate WebApplicationInitializer class, ex);}}}}if (initializers.isEmpty()) {servletContext.log(No Spring WebApplicationInitializer types detected on classpath);return;}servletContext.log(initializers.size() Spring WebApplicationInitializers detected on classpath);AnnotationAwareOrderComparator.sort(initializers);for (WebApplicationInitializer initializer : initializers) {initializer.onStartup(servletContext);}}}代码逻辑也不算复杂检查传入的参数webAppInitializerClasses如果是实现类的话不是接口或抽象类则通过反射机制实例化webAppInitializerClasses并强转为WebApplicationInitializer然后调用WebApplicationInitializer的onStartup方法。
这里有一个小问题参数webAppInitializerClasses实例化之后为什么能强转为WebApplicationInitializer
其实这也是Servlet3.0规范约定的WEB容器会根据HandlesTypes的设置从当前类加载器中查找符合条件的类当前HandlesTypes指定的正是WebApplicationInitializer。
之后的操作就都交给WebApplicationInitializer了。
WebApplicationInitializer
WebApplicationInitializer是承担起Servlet3.0规范约定的初始化Servlet容器的那个人 Interface to be implemented in Servlet 3.0 environments in order to configure the ServletContext programmatically – as opposed to (or possibly in conjunction with) the traditional web.xml-based approach. Implementations of this SPI will be detected automatically by SpringServletContainerInitializer, which itself is bootstrapped automatically by any Servlet 3.0 container. See its Javadoc for details on this bootstrapping mechanism. 我们先看一下他的类结构 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ONu9uceL-1693146405132)(/img/bVc9kPW)] Spring框架实现了WebApplicationInitializer接口的3个抽象类最后一个抽象类AbstractAnnotationConfigDispatcherServletInitializer没有onStartup方法onStartup方法是他的父类实现的。
我们看一下他的父类AbstractDispatcherServletInitializer的onStartup方法 Overridepublic void onStartup(ServletContext servletContext) throws ServletException {super.onStartup(servletContext);registerDispatcherServlet(servletContext);}可以发现他其实是一个模板方法首先调用了父类的onStartup之后调用registerDispatcherServlet方法。父类的onStartup方法是实现rootApplicationContext调用的至于什么是rootApplicationContext我们暂时不管我们先看一下registerDispatcherServlet方法
protected void registerDispatcherServlet(ServletContext servletContext) {String servletName getServletName();Assert.hasLength(servletName, getServletName() must not return null or empty);WebApplicationContext servletAppContext createServletApplicationContext();Assert.notNull(servletAppContext, createServletApplicationContext() must not return null);FrameworkServlet dispatcherServlet createDispatcherServlet(servletAppContext);Assert.notNull(dispatcherServlet, createDispatcherServlet(WebApplicationContext) must not return null);dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());...省略代码首先调用createServletApplicationContext创建ServletApplicationContextServlet容器,之后创建DispathcerServlet并且把创建好的Servlet容器传递给DispatcherServletDispatcherServlet要记录他所在的ServletApplicationContext。
关键代码出现了Servlet容器的创建过程应该就在这个createServletApplicationContext方法中是在AbstractAnnotationConfigDispatcherServletInitializer中实现的 Overrideprotected WebApplicationContext createServletApplicationContext() {AnnotationConfigWebApplicationContext context new AnnotationConfigWebApplicationContext();Class?[] configClasses getServletConfigClasses();if (!ObjectUtils.isEmpty(configClasses)) {context.register(configClasses);}return context;}创建了一个新的AnnotationConfigWebApplicationContext对象然后调用getServletConfigClasses()方法获取配置类之后把配置类注册到了新创建的AnnotationConfigWebApplicationContext对象中。这个getServletConfigClasses()方法是没有实现的应该是我们的实现类需要实现的。
至此Spring通过Servlet3.0规范进行初始化的过程应该已经很清晰了 1.Spring Framework通过WebApplicationInitializer接口的onStartup方法完成Servlet上下文的初始化。 2.Spring Framework已经完成了WebApplicationInitializer接口的大部分实现提供了3个抽象类已经通过模板方法完成了大部分的初始化操作。 3. 猜测应用层只需要扩展AbstractAnnotationConfigDispatcherServletInitializer类实现getServletConfigClasses()方法、返回Servlet的配置类即可完成初始化。
接下来我们就验证一下上述猜测。
创建基于注解的Spring MVC应用
按照上述猜测我们只需要扩展AbstractAnnotationConfigDispatcherServletInitializer、实现getServletConfigClasses()方法即可。
我们还是用上篇文章中用过的例子来验证在动手之前由于注解方式和web.xml配置方式是冲突的配置方式会覆盖掉注解方式所以我们需要删掉web.xml文件copy出去即可。
创建一个configuration包并创建配置类只要配置Controller的扫描路径即可
package org.example.configuration;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;Configuration
ComponentScan(org.example.controller)
public class MvcConfiguration {
}然后再创建initializer的实现类
package org.example.configuration;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;public class MvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {Overrideprotected Class?[] getRootConfigClasses() {return new Class[0];}Overrideprotected Class?[] getServletConfigClasses() {return new Class[] {MvcConfiguration.class};}Overrideprotected String[] getServletMappings() {return new String[] {/};}
}其中RootConfigClasse()方法是为RootApplicationContext服务的我们前面说过了展示不管这个RootApplicationContext是什么现在依然也不管他。
getServletConfigClasses方法返回我们创建的初始化类。
还有一个getServletMappings方法上面没有提到过其实是起到web.xml中的servlet-mapping配置的作用的所以我们直接返回/ - 默认匹配规则。
启动项目访问
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PbumTEUE-1693146405132)(/img/bVc9kTd)]
大功告成
上一篇 Spring MVC 二 基于xml配置