ServletContainerInitializer与@HandlesTypes

实现ServletContainerInitializer接口的类,在jar包中通过在META-INF\services\下的一个叫做javax.servlet.ServletContainerInitializer的文件中指明后在servlet容器启动时候会被容器发现.

SpringServletContainerInitializer java doc   JAR Services API ServiceLoader.load(Class) method detecting the  spring-web module‘s META-INF/services/javax.servlet.ServletContainerInitializer service provider configuration file. See the  JAR Services API documentation as well as section 8.2.4 of the Servlet 3.0 Final Draft specification for complete details.



@HandlesTypes:

This annotation is used to declare an array of application classes which are passed to a javax.servlet.ServletContainerInitializer.

springServletContainerInitializer.onStartup(webAppInitializerClasses, servletContext); java doc:

Delegate the ServletContext to any WebApplicationInitializer implementations present on the application classpath.

Because this class declares @HandlesTypes(WebApplicationInitializer.class), Servlet 3.0+ containers will automatically scan the classpath for implementations of Spring‘s WebApplicationInitializer interface and provide the set of all such types to the webAppInitializerClasses parameter of this method.

If no 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 ServletContainerInitializer has indeed been invoked but that no WebApplicationInitializer implementations were found.

Assuming that one or more WebApplicationInitializer types are detected, they will be instantiated (and sorted if the @@Order annotation is present or the Ordered interface has been implemented). Then the WebApplicationInitializer.onStartup(ServletContext) method will be invoked on each instance, delegating the ServletContext such that each instance may register and configure servlets such as Spring‘s  DispatcherServlet, listeners such as Spring‘s ContextLoaderListener, or any other Servlet API componentry such as filters.

通过使用@HandlesTypes注解配置运行时/共享库的可插拔性.

时间: 2024-10-05 05:07:09

ServletContainerInitializer与@HandlesTypes的相关文章

Servlet初始化与异步支持

Shared libraries(共享库) / runtimes pluggability(运行时插件能力) 1.Servlet容器启动会扫描,当前应用里面每一个jar包的 ServletContainerInitializer的实现2.提供ServletContainerInitializer的实现类: 必须绑定在,META-INF/services/javax.servlet.ServletContainerInitializer文件的内容就是ServletContainerInitiali

Spring 注解驱动(二)WEB 注解开发

Spring 注解驱动(二)WEB 注解开发 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.基本使用 在 Servlet 3.0 时支持注解启动,不再需要 web.xml 配制文件. 1.1 Servlet 3.0 注解 Servlet 3.0 常用注解: @WebServlet @WebFilter @WebInitParam @WebListener @WebServlet("/hello") pu

context创建过程解析(一)之deployDescriptors

总结:主要是创建Context对象,并且将默认context配置,host级别配置,context配置的值设置进去,设置docBase,如果是war包就解压到webapp的目录中,重新设置docBase为war包解压后的目录.如果配置文件中没有指定docBase,那么就以webapps为基路径+context的baseName作为docBase HostConfig.deployApps() //在监听到start事件类型,也就是StandardHost调用startInternal prote

重新学习Spring注解——servlet3.0

53.servlet3.0-简介&测试 54.servlet3.0-ServletContainerInitializer 55.servlet3.0-ServletContext注册三大组件 1.Servlet容器启动会扫描,当前应用里面每一个jar包的 ServletContainerInitializer的实现 2.提供ServletContainerInitializer的实现类: 必须绑定在,META-INF/services/javax.servlet.ServletContaine

Caused by: java.lang.ClassCastException: org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer错误解决办法

严重: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"] java.net.BindException: Address already in use <null>:8080 at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:407) at org.apache.tomcat.util.

Caused by: java.lang.ClassCastException: org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer

A child container failed during startjava.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/book_shop]] at java.util.concurrent.Futur

SpringMVC4零配置

这里对SpringMVC零配置做一个简单的说明 spring4中提供了大量的注解来支持零配置,简要说明如下: @Configuration : 类似于spring配置文件,负责注册bean,对应的提供了@Bean注解.需要org.springframework.web.context.support.AnnotationConfigWebApplicationContext注册到容器中. @ComponentScan : 注解类查找规则定义 <context:component-scan/>

Servlet3.0注解新特性笔记 (转载IBM)

Servlet 3.0 新特性概述 Servlet 3.0 作为 Java EE 6 规范体系中一员,随着 Java EE 6 规范一起发布.该版本在前一版本(Servlet 2.5)的基础上提供了若干新特性用于简化 Web 应用的开发和部署.其中有几项特性的引入让开发者感到非常兴奋,同时也获得了 Java 社区的一片赞誉之声: 异步处理支持:有了该特性,Servlet 线程不再需要一直阻塞,直到业务处理完毕才能再输出响应,最后才结束该 Servlet 线程.在接收到请求之后,Servlet 线

Servlet 3.0 新特性详解

转自:https://www.ibm.com/developerworks/cn/java/j-lo-servlet30/ Servlet 3.0 新特性详解 张 建平2010 年 4 月 23 日发布 WeiboGoogle+用电子邮件发送本页面 6 Servlet 3.0 新特性概述 Servlet 3.0 作为 Java EE 6 规范体系中一员,随着 Java EE 6 规范一起发布.该版本在前一版本(Servlet 2.5)的基础上提供了若干新特性用于简化 Web 应用的开发和部署.其