常规容器下SpringBootServletInitializer如何实现web.xml作用解析
-
public abstract class SpringBootServletInitializer implements WebApplicationInitializer{
-
..................
-
public void onStartup(ServletContext servletContext) throws ServletException {
-
this.logger = LogFactory.getLog(super.getClass());
-
WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
-
-
if (rootAppContext != null) {
-
servletContext.addListener(new ContextLoaderListener(rootAppContext) {
-
public void contextInitialized(ServletContextEvent event) {
-
}
-
});
-
} else
-
this.logger.debug(
-
"No ContextLoaderListener registered, as createRootApplicationContext() did not return an application context");
-
}
-
....................
-
}
- <code class="language-java"><span style="font-size:14px;">@HandlesTypes({ WebApplicationInitializer.class })
- public class SpringServletContainerInitializer implements <span style="background-color:rgb(255,255,255);">ServletContainerInitializer </span>{
- public void onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
- throws ServletException {
- List initializers = new LinkedList();
- 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);
- }
- }</span></code>
-
@HandlesTypes({ WebApplicationInitializer.class })
-
public class SpringServletContainerInitializer implements ServletContainerInitializer {
-
public void onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
-
throws ServletException {
-
List initializers = new LinkedList();
-
-
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);
-
}
-
}
-
package com.shf.springboot.config;
-
-
import javax.servlet.ServletContext;
-
import javax.servlet.ServletException;
-
import org.slf4j.Logger;
-
import org.slf4j.LoggerFactory;
-
import org.springframework.core.annotation.Order;
-
import org.springframework.web.WebApplicationInitializer;
-
import com.shf.springboot.runner.MyStartupRunner1;
-
@Order(1)
-
public class MyWebApplicationInitializer implements WebApplicationInitializer {
-
private Logger logger=LoggerFactory.getLogger(MyStartupRunner1.class);
-
-
@Override
-
public void onStartup(ServletContext paramServletContext) throws ServletException {
-
logger.info("启动加载自定义的MyWebApplicationInitializer");
-
System.out.println("启动加载自定义的MyWebApplicationInitializer");
-
}
-
-
}
-
package com.shf.springboot.servlet;
-
-
import java.io.IOException;
-
import javax.servlet.ServletContext;
-
import javax.servlet.ServletException;
-
import javax.servlet.annotation.WebServlet;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
import org.springframework.boot.web.servlet.ServletContextInitializer;
-
-
public class Servlet4 extends HttpServlet {
-
private static final long serialVersionUID = -4186518845701003231L;
-
-
@Override
-
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-
System.out.println("Servlet4");
-
resp.setContentType("text/html");
-
resp.getWriter().write("Servlet4");
-
}
-
-
@Override
-
public void init() throws ServletException {
-
super.init();
-
System.out.println("Servlet4 loadOnStart");
-
}
-
-
}
-
package com.shf.springboot.config;
-
-
import java.util.Set;
-
import javax.servlet.ServletContainerInitializer;
-
import javax.servlet.ServletContext;
-
import javax.servlet.ServletException;
-
import javax.servlet.ServletRegistration;
-
import org.slf4j.Logger;
-
import org.slf4j.LoggerFactory;
-
-
public class MyServletContainerInitializer implements ServletContainerInitializer {
-
private Logger logger=LoggerFactory.getLogger(MyServletContainerInitializer.class);
-
-
@Override
-
public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
-
logger.info("启动加载自定义的MyServletContainerInitializer");
-
System.out.println("启动加载自定义的MyServletContainerInitializer");
-
ServletRegistration.Dynamic testServlet=servletContext.addServlet("servlet4","com.shf.springboot.servlet.Servlet4");
-
testServlet.setLoadOnStartup(1);
-
testServlet.addMapping("/servlet4");
-
}
-
}
6.3、对新增的servlet设置其请求路径,同时打成WAR包部署至tomcat启动服务,但请求http://localhost:8080/SpringBoot1/servlet4却失败,此时发现需要了解servlet3对于ServletContainerInitializer
的加载机制是如何的,在官方有类似这样的描述“该接口的实现必须声明一个JAR资源放到程序中的META-INF/services下,并且记有该接口实现类的全路径,才会被运行时(server)的查找机制或是其它特定机制找到”。那么我们先参考spring-web-4.3.2.RELEASE.jar中
常规容器下SpringBootServletInitializer如何实现web.xml作用解析的更多相关文章
- 转 web项目中的web.xml元素解析
转 web项目中的web.xml元素解析 发表于1年前(2014-11-26 15:45) 阅读(497) | 评论(0) 16人收藏此文章, 我要收藏 赞0 上海源创会5月15日与你相约[玫瑰里 ...
- javaweb项目中关于配置文件web.xml的解析
一..启动tomcat,加载项目中的web.xml文件,创建servercontext上下文对象. 可以通过servercontext对象在应用中获取web.xml文件中的值. web应用加载的顺序与 ...
- 使用Eclipse创建Web项目时WEB-INF下找不到web.xml问题详解
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/yjrguxing/article/deta ...
- ssm web.xml配置解析
以下为web.xml的配置<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=& ...
- ssm web.xml文件解析
转 以下为web.xml的配置<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:x ...
- javaweb学习总结十七(web应用组织结构、web.xml作用以及配置虚拟主机搭建网站)
一:web应用组织结构 1:web应用组成结构 2:安装web组成机构手动创建一个web应用程序目录 a:在webapps下创建目录web b:在web目录下创建html.jsp.css.js.WEB ...
- struts2中struts.xml和web.xml文件解析及工作原理
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp ...
- JSF技术web.xml配置解析
对Java tutorial-examples中jsf hell1的web.xml配置文件的解析 <?xml version="1.0" encoding="UTF ...
- hello1 web项目中web.xml作用分析
该web.xml文件包含Facelets应用程序所需的几个元素.使用NetBeans IDE创建应用程序时,将自动创建以下所有内容. 指定项目阶段的上下文参数: <context-param&g ...
随机推荐
- Dcloud课程6 php脚本如何在Linux下定时更新数据
Dcloud课程6 php脚本如何在Linux下定时更新数据 一.总结 一句话总结:linux下用crontab命令实现定时任务. 1.linux下执行php脚本用什么命令? 直接用php命令php ...
- cocos2D(一)----第一个cocos2D程序
简单介绍 我们这个专题要学习的是一款iOS平台的2D游戏引擎cocos2d.严格来说叫做cocos2d-iphone,由于cocos2d有非常多个版本号.我们学习的是iphone版本号的.既然是个游戏 ...
- CNTK 搞深度学习-1
CNTK 搞深度学习 Computational Network Toolkit (CNTK) 是微软出品的开源深度学习工具包.本文介绍CNTK的基本内容,如何写CNTK的网络定义语言,以及跑通一个简 ...
- 【例题 6-5 UVA 12657 】Boxes in a Line
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 双向链表模拟题. 移动的时候,要注意它就在所需要的位置的情况.那种情况不移动. (如果已经在所需位置了,还用链表的插入方式强行移动的 ...
- IOS使用AsyncSocket进行Socket通信
首先导入CFNetwork.framework框架 1.下载ASyncSocket库源码 2.把ASyncSocket库源码加入项目 3.在项目增加CFNetwork框架 使用AsyncSocket开 ...
- 手把手教你----MyEclipse中 配置 Tomcat
电脑上配置Tomcatserver 安装Tomcat并配置环境变量 測试是否配置成功 MyEclipse中配置Tomcat 想要开发Java Web的程序.首先在MyEclipse中必须配置Tomca ...
- PHP SPL标准库之数据结构栈(SplStack)介绍(基础array已经可以解决很多问题了,现在开始解决问题)
PHP SPL标准库之数据结构栈(SplStack)介绍(基础array已经可以解决很多问题了,现在开始解决问题) 一.总结 SplStack就是继承双链表(SplDoublyLinkedList)实 ...
- PatentTips - Data Plane Packet Processing Tool Chain
BACKGROUND The present disclosure relates generally to systems and methods for providing a data plan ...
- 数据类型总结——null和undefined
相关文章 简书原文:https://www.jianshu.com/p/c3e252efe848 数据类型总结——概述:https://www.cnblogs.com/shcrk/p/9266015. ...
- putty-gns3
hcl-cloud用的就是这个putty http://forum.gns3.net/topic5016.html File comment: Compiled PuTTY 0.62 for wind ...