常规容器下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 ...
随机推荐
- 调色板原理 & 编程
调色板原理 & 编程 逻辑调色板结构LOGPALETTE,该结构定义如下: typedef struct tagLOGPALETTE { WORD palVersion; //调色板的板本号, ...
- 快速创建SSH信任实现无密码登录
1. 生成本机的公私钥匙对[oracle@Oracle11_2 scripts]$ -t rsa Generating public/private rsa key pair. Enter file ...
- 微服务实战(四):服务发现的可行方案以及实践案例 - DockOne.io
原文:微服务实战(四):服务发现的可行方案以及实践案例 - DockOne.io 这是关于使用微服务架构创建应用系列的第四篇文章.第一篇介绍了微服务架构的模式,讨论了使用微服务架构的优缺点.第二和第三 ...
- NSNotificationCenter消息通信(KVO)
NSNotificationCenter是程序不同类间的消息通信. 注册消息通知: [[NSNotificationCenter defaultCenter]addObserver:self sele ...
- libjpeg用法
libjpeg是一个完全用C语言编写的库,包含了被广泛使用的JPEG解码.JPEG编码和其他的JPEG功能的实现.这个库由独立JPEG工作组维护.最新版本号是6b,于1998年发布.可以参考维基百科关 ...
- [PReact] Integrate Redux with Preact
Redux is one of the most popular state-management libraries and although not specific to React, it i ...
- jQuery常用方法(持续更新)(转)
0.常用代码: 请容许我在1之前插入一个0,我觉得我有必要把最常用的代码放在第一位,毕竟大部分时间大家都是找代码的. (1)AJAX请求 $(function() { $('#send').click ...
- 使用JQuery将前端form表单数据转换为JSON字符串传递到后台处理
一般地,我们在处理表单(form表单哦)数据时,传输对象或字符串到后台,Spring MVC或SpringBoot的Controller接收时使用一个对象作为参数就可以被正常接收并封装到对象中.这种方 ...
- form表单嵌套,用标签的form属性来解决表单嵌套的问题
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- NET WinForm 开发所见即所得的 IDE 开发环境
Github 开源:使用 .NET WinForm 开发所见即所得的 IDE 开发环境(Sheng.Winform.IDE)[2.源代码简要说明] GitHub:https://github.co ...