`

究 Spring 3.1之无web.xml式 基于代码配置的servlet3.0应用(转)

阅读更多

  大家应该都已经知道Spring 3.1对无web.xml式基于代码配置的servlet3.0应用。通过spring的api或是网络上高手们的博文,也一定很快就学会并且加到自己的应用中去了。PS:如果还没,也可以小小参考一下鄙人的上一篇文章<<探 Spring 3.1之无web.xml式 基于代码配置的servlet3.0应用>>。 

        经过一天的深度research, 我了解,理解以及重现了springframework的那一小段代码。
 

        OK,第一步,入手点,WebApplicationInitializer接口。因为我们只需实现这个接口覆写它的一个方法,就可以做到配置web.xml同样的功效。看它的源码,其实看和不看没什么两样: 

Java代码  收藏代码
  1. package org.springframework.web;  
  2.   
  3. import javax.servlet.ServletContext;  
  4. import javax.servlet.ServletException;  
  5. public interface WebApplicationInitializer {  
  6.     void onStartup(ServletContext servletContext) throws ServletException;  
  7. }  

   就这么点儿,有效代码5行,弄地我一头雾水,就是一个普通接口,声明了一个方法。连注解都没有,server是怎么找到实现了它的类的?如果这样,何不找我定义的其它接口(的实现类完成配置工作)呢。可见现在java的解耦技术,真令人汗颜。 
   第二步,这个接口旁边(同包)有个SpringServletContainerInitializer, 看下它是何方神圣吧:
 
Java代码  收藏代码
  1. package org.springframework.web;  
  2.   
  3. import java.lang.reflect.Modifier;  
  4. import java.util.Collections;  
  5. import java.util.LinkedList;  
  6. import java.util.List;  
  7. import java.util.ServiceLoader;  
  8. import java.util.Set;  
  9. import javax.servlet.ServletContainerInitializer;  
  10. import javax.servlet.ServletContext;  
  11. import javax.servlet.ServletException;  
  12. import javax.servlet.annotation.HandlesTypes;  
  13.   
  14. import org.springframework.core.annotation.AnnotationAwareOrderComparator;  
  15.   
  16. @HandlesTypes(WebApplicationInitializer.class)  
  17. public class SpringServletContainerInitializer implements ServletContainerInitializer {  
  18. public void onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)  
  19.             throws ServletException {  
  20.   
  21.         List<WebApplicationInitializer> initializers = new LinkedList<WebApplicationInitializer>();  
  22.         if (webAppInitializerClasses != null) {  
  23.             for (Class<?> waiClass : webAppInitializerClasses) {  
  24.                 // Be defensive: Some servlet containers provide us with invalid classes,  
  25.                 // no matter what @HandlesTypes says...  
  26.                 if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) &&             WebApplicationInitializer.class.isAssignableFrom(waiClass)) {  
  27.                     try {  
  28.                         initializers.add((WebApplicationInitializer) waiClass.newInstance());  
  29.                     }  
  30.                     catch (Throwable ex) {  
  31.                         throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);  
  32.                     }  
  33.                 }  
  34.             }  
  35.         }  
  36.   
  37.         if (initializers.isEmpty()) {  
  38.             servletContext.log("No Spring WebApplicationInitializer types detected on classpath");  
  39.             return;  
  40.         }  
  41.   
  42.         Collections.sort(initializers, new AnnotationAwareOrderComparator());  
  43.         servletContext.log("Spring WebApplicationInitializers detected on classpath: " + initializers);  
  44.   
  45.         for (WebApplicationInitializer initializer : initializers) {  
  46.             initializer.onStartup(servletContext);  
  47.         }  
  48.     }  
  49.   
  50. }  


     以上的有效代码28行。刚看时也很迷茫,其实慢慢就理解了。拟个伪代码吧,方便大家理解: 
      1,定义一个类SpringServletContainerInitializer,并标明该类要操作的一个类WebApplicationInitializer 
      2, 该类会行使ServletContainerInitializer接口的一个行为onStartup,从而将一个集合中的初始化设置 全部配置到ServletContext的实例中。 
      3,具体的onStartup方法中,建立合格配置列表, 
      4,如果确定集合中有配置,逐一检查配置是否是合格配置,具体判断依据:这个类不是接口,不是抽象类,而且是所要操作的那个接口的一个实现类。满足此依据,合格。将合格的配置类实例化放入合格配置列表。过程中有错要通知控制台。 
     5,如若执行完步骤4,发现没有合格配置,在ServletContext记录该结果,并结束onStartup行为。 
     6,将找到配置按一定排列方式(AnnotationAwareOrder)排序。 
     7,在ServletContext中记录找到结果。 
     8,逐一执行配置。 即驱动每一个WebApplicationInitializer的实现类行使其onStartup行为。 

     第三步很明显了,去research 接口ServletContainerInitializer和注解HandleType。在这里:
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContainerInitializer.html 

    该接口允许一个库或运行时,(运行时应该指server)声明为一个web程序的启动状态,并执行任何所需的程序中注册的servlet,filter,listener来响应它...... 
     其它也就不用看了,可以想象得到支持Servlet3机制的服务器,会找到这样接口的实现类,执行onStartup行为。至于如何找,无非也是这样一系列的反射机制的应用。自己做一个试试吧: 
     自定义的WebApplicationInitializer:
 

Java代码  收藏代码
  1. package com.gxino.imagecapture.cfg;  
  2.   
  3. import javax.servlet.ServletContext;  
  4. import javax.servlet.ServletException;  
  5.   
  6. public interface WebParameter {  
  7.     public void loadInfo(ServletContext servletContext) throws ServletException;  
  8. }  


     自定义的ServletContainerInitializer,我做得很简单,直接去执行找到配置类中的loadInfo方法 

Java代码  收藏代码
  1. package com.gxino.imagecapture.cfg;  
  2.   
  3. import java.lang.reflect.Modifier;  
  4. import java.util.Set;  
  5.   
  6. import javax.servlet.ServletContainerInitializer;  
  7. import javax.servlet.ServletContext;  
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.annotation.HandlesTypes;  
  10.   
  11. @HandlesTypes(WebParameter.class)  
  12. public class WebConfiguration implements ServletContainerInitializer {  
  13.   
  14.     @Override  
  15.     public void onStartup(Set<Class<?>> webParams, ServletContext servletCtx)  
  16.             throws ServletException {  
  17.         if (webParams != null) {  
  18.             for (Class<?> paramClass : webParams) {  
  19.                 if (!paramClass.isInterface() && !Modifier.isAbstract(paramClass.getModifiers()) &&  
  20.                         WebParameter.class.isAssignableFrom(paramClass)) {  
  21.                     try {  
  22.                         ((WebParameter) paramClass.newInstance()).loadInfo(servletCtx);  
  23.                     }  
  24.                     catch (Throwable ex) {  
  25.                         throw new ServletException("Failed to instantiate WebParam class", ex);  
  26.                     }  
  27.                 }  
  28.             }//loop  
  29.         }//Web Params  
  30.     }//onStartup  
  31.   
  32. }  

      写个测试Servlet: 

Java代码  收藏代码
  1. package com.gxino.imagecapture.ctrl;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9.   
  10. import com.gxino.imagecapture.cfg.WebParameter;  
  11.   
  12. public class TestServlet extends HttpServlet {  
  13.       
  14.     public void doGet(HttpServletRequest req, HttpServletResponse resp){  
  15.         System.out.println("Some client access once");  
  16.         try {  
  17.             req.getRequestDispatcher("/index.jsp").forward(req, resp);  
  18.         } catch (ServletException | IOException e) {  
  19.             // TODO Auto-generated catch block  
  20.             e.printStackTrace();  
  21.         }  
  22.     }  
  23.       
  24. }  


       实现WebParam配置接口来配置刚才的Servlet: 

Java代码  收藏代码
  1. package com.gxino.imagecapture.cfg;  
  2.   
  3. import javax.servlet.ServletContext;  
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.ServletRegistration;  
  6.   
  7.   
  8. public class ServletParameter implements WebParameter {  
  9.   
  10.     @Override  
  11.     public void loadInfo(ServletContext servletContext) throws ServletException {  
  12.         ServletRegistration.Dynamic testServlet=servletContext.addServlet("test","com.gxino.imagecapture.ctrl.TestServlet");  
  13.         testServlet.setLoadOnStartup(1);  
  14.         testServlet.addMapping("/index.html");  
  15.     }  
  16.   
  17. }  

     启动服务器,访问http://localhost:xxxx/xxxxx/index.html 
  
     失败。Debug. 发现没有走这些代码。应该还差关键环节。看来还得知道Servlet3中是怎么找ServletContainerInitializer的。再回刚才ServletContainerInitializer的api有这样一句:该接口的实现必须声明一个JAR资源放到程序中的META-INF/services下,并且记有该接口那个实现类的全路径,才会被运行时(server)的查找机制或是其它特定机制找到。那篇api需要仔细阅读啊。 
     到org.springframework.web-3.0.1.RELEASE.jar中能找到META-INF/services下的javax.servlet.ServletContainerInitializer文件,内容为org.springframework.web.SpringServletContainerInitializer同样,我们专门作这样一个包,在mkdir好的META-INF/services下vi 一个文件命名为javax.servlet.ServletContainerInitializer,内容为自定的那个WebConfiguration的全路径类名。 然后在META-INF的parent路径下运行jar cvf test.jar META-INF。一切完毕,将其放到WEB-INF/lib下。启动。
 
     
     这回大功告成。 
     
     访问http://localhost:xxxx/xxxxx/index.html。页面跳到了index.jsp下。 
     并且控制台打出: Some client access once 

     再使个劲,将Servlet和Servlet配置合二为一:
 
Java代码  收藏代码
  1. package com.gxino.imagecapture.ctrl;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletContext;  
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.ServletRegistration;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import com.gxino.imagecapture.cfg.WebParameter;  
  13.   
  14. public class TestServlet extends HttpServlet implements WebParameter{  
  15.   
  16.     @Override  
  17.     public void loadInfo(ServletContext servletContext) throws ServletException {  
  18.         ServletRegistration.Dynamic testServlet=servletContext.addServlet("test""com.gxino.imagecapture.ctrl.TestServlet");  
  19.         testServlet.setLoadOnStartup(1);  
  20.         testServlet.addMapping("/index.html");  
  21.     }  
  22.     public void doGet(HttpServletRequest req, HttpServletResponse resp){  
  23.         System.out.println("Some client access once");  
  24.         try {  
  25.             req.getRequestDispatcher("/index.jsp").forward(req, resp);  
  26.         } catch (ServletException | IOException e) {  
  27.             // TODO Auto-generated catch block  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31.       
  32. }  


这回我们看到,配置文件与servlet放到了一起。这样将回节省大量时间。 

    以后直接运用Spring Framework的WebApplicationInitializer也知道是怎么一回事儿了。而且可以将Spring 的applicationContext.xml与web.xml融合在一个类中。即注解为@Configuration,并实现WebApplicationInitializer.回头试试。

分享到:
评论

相关推荐

    spring-boot-reference.pdf

    27.1. The “Spring Web MVC Framework” 27.1.1. Spring MVC Auto-configuration 27.1.2. HttpMessageConverters 27.1.3. Custom JSON Serializers and Deserializers 27.1.4. MessageCodesResolver 27.1.5. Static...

    SpringSecurity 3.0.1.RELEASE.CHM

    2.2.1. 配置web.xml 2.2.2. 最小 配置 2.2.2.1. auto-config包含了什么? 2.2.2.2. 表单和基本登录选项 2.2.3. 使用其他认证提供器 2.2.3.1. 添加一个密码编码器 2.3. 高级web特性 2.3.1. Remember-Me认证 ...

    springboot参考指南

    创建一个非web(non-web)应用 ii. 63. 属性&配置 i. 63.1. 外部化SpringApplication配置 ii. 63.2. 改变应用程序外部配置文件的位置 iii. 63.3. 使用'short'命令行参数 iv. 63.4. 使用YAML配置外部属性 v. 63.5. ...

    Spring中文帮助文档

    2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 ...

    Spring API

    2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 ...

    Spring Security 中文教程.pdf

    2.2.1. 配置web.xml 2.2.2. 最小 &lt;http&gt; 配置 2.2.2.1. auto-config 包含了什么? 2.2.2.2. 表单和基本登录选项 2.2.3. 使用其他认证提供器 2.2.3.1. 添加一个密码编码器 2.3. 高级web特性 2.3.1. ...

    DWR.xml配置文件说明书(含源码)

    DWR.xml配置文件说明书 1、 建立dwr.xml 配置文件 任何一个dwr.xml的文件都需要包含DWR DOCTYPE的声明行,格式如下: &lt;!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" ...

    Spring Security-3.0.1中文官方文档(翻译版)

    2.2.1. 配置web.xml 2.2.2. 最小&lt;http&gt; 配置 2.2.2.1. auto-config 包含了什么? 2.2.2.2. 表单和基本登录选项 2.2.3. 使用其他认证提供器 2.2.3.1. 添加一个密码编码器 2.3. 高级web 特性 2.3.1. ...

    spring-framework-reference-4.1.2

    3.1. Improved Getting Started Experience .................................................................. 17 3.2. Removed Deprecated Packages and Methods ...............................................

    spring-framework-reference4.1.4

    3.1. Improved Getting Started Experience .................................................................. 17 3.2. Removed Deprecated Packages and Methods ...............................................

    spring和hibernate__jar包,详细说明看jar包列表

    org.springframework.web.servlet-3.1.1.RELEASE.jar org.springframework.web.struts-3.1.1.RELEASE.jar slf4j-api-1.5.8.jar slf4j-log4j12-1.5.8.jar spring-aop-3.1.1.RELEASE.jar spring-asm-3.1.1.RELEASE.jar...

    Spring 3.x 中文开发手册.pdf

    10、支持Servlet3的某个东东,可以写程序直接启动webapp,而非web.xml,不感兴趣 11、支持servlet3的上传东东,可能是对现有MultipartResolver的加强 12、JPA什么,直接无视 以下都是springmvc的加强,可以注意了 ...

    spring-mvc3-javaconfig:配置为不使用XML的Java Spring MVC 3应用程序。 还使用Servlet 3 API绕过web.xml

    Spring @MVC 3.1 Java配置这是一个使用Spring的Java配置而不是XML的简单示例。 我的解释了此示例。 这是一个伪造的登录应用程序。 在现实生活中,您可能会改用Spring Security。 有一个服务和数据访问层,但是存储库...

    Spring.3.x企业应用开发实战(完整版).part2

    Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。  Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (1)

    10.3.4 建立Spring的配置文档dispatcherServlet-servlet.xml 10.3.5 配置web.xml 10.3.6 启动Tomcat运行程序 10.4 Spring 的视图(View) 10.4.1 视图简介 10.4.2 视图解析 10.5 Spring的控制器(Controller) ...

    struts2.4+spring3.1+hibernate4.1的SSH框架

    SSH 为 struts2.4+spring3.1+hibernate4.1的一个集成框架,是目前较流行的一种Web应用程序开源框架。  集成SSH框架的系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层,以帮助开发人员在短期内搭建...

    271个java需要用的jar包

    servlet.jar shiro-cas-1.2.2.jar shiro-core-1.2.2.jar shiro-spring-1.2.2.jar shiro-web-1.2.2.jar sitemesh-2.4.2.jar slf4j-api-1.3.1.jar slf4j-api-1.6.0.jar slf4j-api-1.7.5.jar slf4j-log4j12-1.7.7.jar ...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (2)

    10.3.4 建立Spring的配置文档dispatcherServlet-servlet.xml 10.3.5 配置web.xml 10.3.6 启动Tomcat运行程序 10.4 Spring 的视图(View) 10.4.1 视图简介 10.4.2 视图解析 10.5 Spring的控制器(Controller) ...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (3)

    10.3.4 建立Spring的配置文档dispatcherServlet-servlet.xml 10.3.5 配置web.xml 10.3.6 启动Tomcat运行程序 10.4 Spring 的视图(View) 10.4.1 视图简介 10.4.2 视图解析 10.5 Spring的控制器(Controller) ...

    Spring3.x企业应用开发实战(完整版) part1

    Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。  Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...

Global site tag (gtag.js) - Google Analytics