聊聊、Spring ServletContainerInitializer

摘要:
我们不使用web XML来配置servlet,而是使用Java代码来继承AbstractAnnotationConfigDispatcherServletInitializer。让我们来谈谈Spring代码级逻辑。首先,javax。servlet。ServletContainerInitializer接口类@HandlesTypes@Target @ Retentionpublic@interfaceHandlesTypes{类[]值();}让我们来看看。Spring web xxx下的META-INF/services文件夹。jar包包含一个类似javax的文件。servlet。ServletContainerInitializer,其中包含org.springframework。web SpringServletContainerInitializer您可能需要了解这里的JDKSPI机制。SPI的全称是。它是一种内置于JDK中的服务提供发现机制。也就是说,在启动容器之后,org.springframework.web中的onStartup方法。将调用SpringServletContainerInitializer。

  我们平时用 Java 注解很多,例如 @Configuration、@Component、@Service,我们习惯于通过 XML 方式来实现 Web,而用 Java 注解方式来实现 Web 却很少。今天来说下,Spring 是怎么通过 Java 注解方式来实现 Web 和 Mvc。

一、实现 WebApplicationInitializer 接口  


public class WebInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext servletContext) throws ServletException {}
}

二、继承 AbstractAnnotationConfigDispatcherServletInitializer 


 public class WebMvcInitializer  extends AbstractAnnotationConfigDispatcherServletInitializer {}

三、容器启动 


  这里必须说下容器,容器就是运行程序的器件。像我们最熟悉的 Tomcat,Jetty 等等,要让容器知道怎么运行你的程序,那么你的程序必须符合容器的规范,或者说遵循规矩。

  Tomcat 是怎么做到的呢?我们不用 web.xml 来配置 Servlet,我们就用 Java 代码,继承 AbstractAnnotationConfigDispatcherServletInitializer 就行了。怎么做到的?

  这里就不去分析 catalina 启动脚本,后面单独分析。

  我们只说下 Spring 代码层面的逻辑,首先 javax.servlet.ServletContainerInitializer 这个接口类。这个类的存在就是为了实现 Java 代码配置。而且是 Servlet 3.0 版本才提供,低于 3.0 都没法用

  Java 代码实现配置。看看源代码 

package javax.servlet;
public interface ServletContainerInitializer {
public void onStartup(Set<Class<?>> c, ServletContext ctx)
        throws ServletException; 
}
    其中有这样一段注解:
* Interface which allows a library /runtime to be notified of a web
* application's startup phase and perform any required programmatic
* registration of servlets, filters, and listeners in response to it.
*
* <p>Implementations of this interface may be annotated with
* {@link javax.servlet.annotation.HandlesTypes HandlesTypes}, in order to
* receive (at their {@link #onStartup} method) the Set of application
* classes that implement, extend, or have been annotated with the class
* types specified by the annotation. 
*
* <p>If an implementation of this interface does not use this annotation,
* or none of the application classes match the ones specified
* by the annotation, the container must pass a <tt>null</tt> Set of classes
* to {@link #onStartup}.
*
* <p>Implementations of this interface must be declared by a JAR file
* resource located inside the <tt>META-INF/services</tt> directory and
* named for the fully qualified class name of this interface, and will be 
* discovered using the runtime's service provider lookup mechanism
* or a container specific mechanism that is semantically equivalent to
* it. In either case, ServletContainerInitializer services from web
* fragment JAR files excluded from an absolute ordering must be ignored,
* and the order in which these services are discovered must follow the
* application's classloading delegation model. 

  意思是,这个接口是为了程序启动时,实现 Servlet,Filter,Listener 注册的,但是实现这个接口的类必须在一个 Jar包 的 META-INF/services 目录下面的 javax.servlet.ServletContainerInitializer 文件中有定义。而且实现类必须有 @HandlesTypes 注解。什么意思? 

@HandlesTypes
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface HandlesTypes {
    Class[] value();
}   

  我们来看看,Spring-web-xxx.jar 包下面的 META-INF/services 文件夹,就有 javax.servlet.ServletContainerInitializer 这么一个文件,里面内容是 org.springframework.web.SpringServletContainerInitializer。这里可能需要了解JDK SPI机制,SPI全称为(Service Provider Interface) ,是JDK内置的一种服务提供发现机制。后面文章单独分析。也就是说,启动容器之后,会调用org.springframework.web.SpringServletContainerInitializer中的onStartup方法。

  那么 org.springframework.web.SpringServletContainerInitializer 这个类又是什么样?来看看

@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
@Override
 public void onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
   throws ServletException {
    List<WebApplicationInitializer> initializers = new LinkedList<WebApplicationInitializer>();
  if (webAppInitializerClasses != null) {
   for (Class<?> waiClass : webAppInitializerClasses) {
    if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) &&
      WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
     try {
      initializers.add((WebApplicationInitializer) 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);
  }
   }
} 

  有这么一段注解:

 * Delegate the {@code ServletContext} to any {@link WebApplicationInitializer}
 * implementations present on the application classpath.
 * <p>Because this class declares @{@code HandlesTypes(WebApplicationInitializer.class)},
 * Servlet 3.0+ containers will automatically scan the classpath for implementations
 * of Spring's {@code WebApplicationInitializer} interface and provide the set of all
 * such types to the {@code webAppInitializerClasses} parameter of this method.
 * <p>If no {@code WebApplicationInitializer} implementations are found on the classpath,
 * this method is effectively a no-op. An INFO-level log message will be issued notifying
 * the user that the {@code ServletContainerInitializer} has indeed been invoked but that
 * no {@code WebApplicationInitializer} implementations were found.
 * <p>Assuming that one or more {@code WebApplicationInitializer} types are detected,
 * they will be instantiated (and <em>sorted</em> if the @{@link
 * org.springframework.core.annotation.Order @Order} annotation is present or
 * the {@link org.springframework.core.Ordered Ordered} interface has been
 * implemented). Then the {@link WebApplicationInitializer#onStartup(ServletContext)}
 * method will be invoked on each instance, delegating the {@code ServletContext} such
 * that each instance may register and configure servlets such as Spring's
 * {@code DispatcherServlet}, listeners such as Spring's {@code ContextLoaderListener},
 * or any other Servlet API componentry such as filters.
 * @param webAppInitializerClasses all implementations of
 * {@link WebApplicationInitializer} found on the application classpath
 * @param servletContext the servlet context to be initialized
 * @see WebApplicationInitializer#onStartup(ServletContext)
 * @see AnnotationAwareOrderComparator

   原来如此,Servlet 3.0+  容器会自动去扫描 classpath 下所有实现了 WebApplicationInitializer 接口的类,如果没有找到相关类,那么 An INFO-level log message will be issued notifying the user,会有一条INFO级别 的日志。

  如果找到了多个实现类,那么都会被实例化,如果实现了 org.springframework.core.Ordered 接口或者添加了 @Order注解,那么就按照顺序来。

(1)org.springframework.core.Ordered
public interface Ordered {
 /**
  * Useful constant for the highest precedence value.
  * @see java.lang.Integer#MIN_VALUE
  */
 int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;
 /**
  * Useful constant for the lowest precedence value.
  * @see java.lang.Integer#MAX_VALUE
  */
 int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
 int getOrder();
}
(2)@Order
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Documented
public @interface Order {
 /**
  * The order value.
  * <p>Default is {@link Ordered#LOWEST_PRECEDENCE}.
  * @see Ordered#getOrder()
  */
 int value() default Ordered.LOWEST_PRECEDENCE;
}

  为什么 @MapperScan 和 @ComponentScan 会被调用?到这里就很清晰了,<mybatis:scan> 和 <context:component-scan> 标签的调用就很简单了,因为我们在 xml 里面配置了。

  下一篇来聊聊,这些注解和标签被调用了,怎么识别的呢?我怎么知道要去扫描这个包下面的类?哪里有定义吗?

免责声明:文章转载自《聊聊、Spring ServletContainerInitializer》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Android 7.0 Gallery图库源码分析1JS实现定时循环上翻下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SpringBoot初学(4)– JdbcTemplate和Mybatis

前言 github: https://github.com/vergilyn/SpringBootDemo 代码位置: 一、Spring Boot集成JdbcTemplate或NamedParameterJdbcTemplate spring boot中JdbcTemplate与NamedParameterJdbcTemplate都是被自动配置的...

将 Spring boot 项目打成可执行Jar包,及相关注意事项(main-class、缺少 xsd、重复打包依赖)

最近在看 spring boot 的东西,觉得很方便,很好用。对于一个简单的REST服务,都不要自己部署Tomcat了,直接在 IDE 里 run 一个包含 main 函数的主类就可以了。 但是,转念一想,到了真正需要部署应用的时候,不可能通过 IDE 去部署啊。那有没有办法将 spring boot 的项目打包成一个可执行的 jar 包,然后通过 ja...

Spring AOP之使用注解创建切面

上节中我们已经定义了Performance接口,他是切面中的切点的一个目标对象。那么现在就让我们使用AspectJ注解来定义切面吧。 1.定义切面 下面我们就来定义一场舞台剧中观众的切面类Audience: package com.spring.aop.service.aop; import org.aspectj.lang.ProceedingJoi...

【spring】spring源码阅读之xml读取、bean注入(BeanFactory)

前言   此源码其实是在4月中旬就看了,而且当初也写了一份word文档,但不打算直接把word发上来。还是跟着以前的笔记、跟踪代码边看边写吧。   其实当初看源码的理由很简单,1、才进新公司,比较有空闲。2、面试老是问spring的问题,我理论又不好,所以想看下。   但现在,我重新看的目的其实不在于其实现原理,而是想学习和写出好的编码风格。(当初大概花了...

【spring】静态资源的访问受限解决方法

前言 我们知道在整合spring mvc框架的时候需要在web.xml中配置一个servlet 代码如下 <!--spring mvc 的DispatcherServlet--> <servlet> <servlet-name>enterprise-servlet</servlet-name...

Spring boot validation校验

使用 Hibernate validator 的步骤:1. 在 Pojo 类的字段上, 加上 Hibernate validator 注解2. 在Controller 函数的形参前加上 @Valid 或 @Validated 注解, 触发一次validation. 3. 在每个 @Valid 或 @Validated 注解参数后, 紧跟一个 Binding...