還沒有web.xml,生

 配置tomcat

嵌入式Servlet容器:应用打成可执行的jar

  优点:简单、便携;  

  缺点:默认不支持JSP、优化定制比较复杂

  使用定制器【ServerProperties、自定义EmbeddedServletContainerCustomizer】,自己编写嵌入式Servlet容器的创建工厂[EmbeddedServletContainerFactory】

外置的Servlet容器:外面安装Tomcat---应用war包的方式打包;

步骤

1)、必须创建一个war项目;(利用idea创建好目录结构)
2)、将嵌入式的Tomcat指定为provided;

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

3)、必须编写一个SpringBootServletInitializer的子类,并调用configure方法

public class ServletInitializer extends SpringBootServletInitializer {
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  //传入SpringBoot应用的主程序
  return application.sources(SpringBoot04WebJspApplication.class);
  }
}

4)、启动服务器就可以使用;

原理


jar包:执行SpringBoot主类的main方法,启动ioc容器,创建嵌入式的Servlet容器;
war包:启动服务器,服务器启动SpringBoot应用【SpringBootServletInitializer】,启动ioc容器;

规则:(參見servlet3.0(Spring注解版)官方文檔:8.2.4 Shared libraries / runtimes pluggability)

1)、服务器启动(web应用启动)会创建当前web应用里面每一个jar包里面ServletContainerInitializer实例

2)、ServletContainerInitializer的实现放在jar包的META-INF/services文件夹下,有一个名为javax.servlet.ServletContainerInitializer的文件,

内容就是 ServletContainerInitializer的实现类的全类名

3)、还可以使用@HandlesTypes,在应用启动的时候加载我们感兴趣的类

流程


1)、启动Tomcat

2)、\org\springframework\spring-web\5.1.2.RELEASE\spring-web-5.1.2.RELEASE.jar! /META-INF/services/javax.servlet.ServletContainerInitializer

package org.springframework.web;  //Spring的web模块里面有这个文件

@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
//3)、 将@HandlesTypes(WebApplicationInitializer.class)标注的所有这个类型的类都传入到onStartup方法的Set<>類型參數
@Override
public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
throws ServletException { List<WebApplicationInitializer> initializers = new LinkedList<>(); if (webAppInitializerClasses != null) {
for (Class<?> waiClass : webAppInitializerClasses) {
// Be defensive: Some servlet containers provide us with invalid classes,
// no matter what @HandlesTypes says...
if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) &&
WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
try {
initializers.add((WebApplicationInitializer)
ReflectionUtils.accessibleConstructor(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); //4)、每一个WebApplicationInitializer都调用自己的onStartup;
}
} }

5)、 創建War方式的jar包時自動生成的類:会被创建对象,并执行onStartup方法

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringbootwarApplication.class); //啓動Spring Boot的主類
} }

6)、創建War方式的jar包時自動生成的類的父類方法onStartup被執行的时候:创建容器

public abstract class SpringBootServletInitializer implements WebApplicationInitializer {
protected Log logger;
private boolean registerErrorPageFilter = true; public SpringBootServletInitializer() {
} protected final void setRegisterErrorPageFilter(boolean registerErrorPageFilter) {
this.registerErrorPageFilter = registerErrorPageFilter;
} public void onStartup(ServletContext servletContext) throws ServletException {
this.logger = LogFactory.getLog(this.getClass());
WebApplicationContext rootAppContext = this.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");
}
}
  //創建IoC容器
protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
SpringApplicationBuilder builder = this.createSpringApplicationBuilder();
builder.main(this.getClass());
ApplicationContext parent = this.getExistingRootWebApplicationContext(servletContext);
if (parent != null) {
this.logger.info("Root context already created (using as parent).");
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, (Object)null);
builder.initializers(new ApplicationContextInitializer[]{new ParentContextApplicationContextInitializer(parent)});
} builder.initializers(new ApplicationContextInitializer[]{new ServletContextApplicationContextInitializer(servletContext)});
builder.contextClass(AnnotationConfigServletWebServerApplicationContext.class);
     
     //调用configure方法,子类重写了这个方法,将SpringBoot的主程序类传入了进来
builder = this.configure(builder);
builder.listeners(new ApplicationListener[]{new SpringBootServletInitializer.WebEnvironmentPropertySourceInitializer(servletContext, null)});
    
     //使用Builder創建Spring應用
SpringApplication application = builder.build();
if (application.getAllSources().isEmpty() && AnnotationUtils.findAnnotation(this.getClass(), Configuration.class) != null) {
application.addPrimarySources(Collections.singleton(this.getClass()));
} Assert.state(!application.getAllSources().isEmpty(), "No SpringApplication sources have been defined. Either override the configure method or add an @Configuration annotation");
if (this.registerErrorPageFilter) {
application.addPrimarySources(Collections.singleton(ErrorPageFilterConfiguration.class));
}
     //开启Spring 应用
return this.run(application);
} protected SpringApplicationBuilder createSpringApplicationBuilder() {
return new SpringApplicationBuilder(new Class[0]);
} protected WebApplicationContext run(SpringApplication application) {
return (WebApplicationContext)application.run(new String[0]);
} private ApplicationContext getExistingRootWebApplicationContext(ServletContext servletContext) {
Object context = servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
return context instanceof ApplicationContext ? (ApplicationContext)context : null;
} protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder;
} private static final class WebEnvironmentPropertySourceInitializer implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
private final ServletContext servletContext; private WebEnvironmentPropertySourceInitializer(ServletContext servletContext) {
this.servletContext = servletContext;
} public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
if (environment instanceof ConfigurableWebEnvironment) {
((ConfigurableWebEnvironment)environment).initPropertySources(this.servletContext, (ServletConfig)null);
} } public int getOrder() {
return -2147483648;
}
}
}

 

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringbootwarApplication.class); //啓動Spring Boot的主類
} }

Spring Boot项目jar包启动类

@SpringBootApplication
public class SpringbootwarApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootwarApplication.class, args);
}
}
public class SpringApplication {
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
this.configureHeadlessProperty();
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting(); Collection exceptionReporters;
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
Banner printedBanner = this.printBanner(environment);
context = this.createApplicationContext();
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);        //刷新Ioc容器,IoC容器的初始化
       this.refreshContext(context);
this.afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
} listeners.started(context);
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
} try {
listeners.running(context);
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
}

总结 (使用外部Servlet容器)


1、 SpringBootServletInitializer – 重写configure

2、SpringApplicationBuilder – builder.source(@SpringBootApplication类)

3、启动原理 – Servlet3.0标准ServletContainerInitializer扫描所有jar包中METAINF/services/javax.servlet.ServletContainerInitializer文件指定的类并加载

  – 加载spring web包下的SpringServletContainerInitializer

  – 扫描@HandleType(WebApplicationInitializer)

  – 加载SpringBootServletInitializer并运行onStartup方法

  – 加载@SpringBootApplication主类,启动容器等

19. SpringBoot_web开发-使用外部Servlet容器&JSP支持的更多相关文章

  1. springboot开发之使用外部servlet容器及对jsp的支持

    一般而言,springboot是使用自己内嵌的servlet容器,比如tomcat等等,而且默认的模板引擎是thymeleaf,那么如何让springboot使用外部的servlet容器并支持对jsp ...

  2. SpringMVC内容略多 有用 熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器、过滤器等Web组件以及MVC架构模式进行Java Web项目开发的经验。

    熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器.过滤器等Web组件以及MVC架构 ...

  3. 对于Servlet、Servlet容器以及一个Servlet容器-Tomcat

    Servlet.Servlet容器等内容讲解 转载自http://blog.csdn.net/iAm333 对于Servlet.Servlet容器以及一个Servlet容器-Tomcat这些概念讲解的 ...

  4. Servlet、Servlet容器等内容讲解

    转载自http://blog.csdn.net/iAm333 对于Servlet.Servlet容器以及一个Servlet容器-Tomcat这些概念讲解的挺清晰的,转载下 之前在开源中国看到一篇文章& ...

  5. java框架之SpringBoot(8)-嵌入式Servlet容器

    前言 SpringBoot 默认使用的嵌入式 Servlet 容器为 Tomcat,通过依赖关系就可以看到: 问题: 如何定制和修改 Servlet 容器相关配置? SpringBoot 能否支持其它 ...

  6. 精尽Spring Boot源码分析 - 支持外部 Tomcat 容器的实现

    该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...

  7. Servlet与Jsp的结合使用实现信息管理系统一

    PS:1:先介绍一下什么是Servlet? Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地 ...

  8. 配置嵌入式Servlet容器

    SpringBoot默认是用的是Tomcat作为嵌入式的Servlet容器:问题?1).如何定制和修改Servlet容器的相关配置:1.修改和server有关的配置(ServerProperties) ...

  9. 18、配置嵌入式servlet容器(2)

    使用其他Servlet容器 -Jetty(长连接) -Undertow(不支持jsp) 替换为其他嵌入式Servlet容器   默认支持: Tomcat(默认使用) Jetty: <depend ...

随机推荐

  1. Win10 打开 ubuntu子系统

    1. 修改windows的设置, 增加开发人员模式 针对开人员模式 使用添加删除程序 添加 ubuntu子系统的角色 运行输入control.. 然后添加删除程序 安装完后重启 运行输入 bash 就 ...

  2. SAP顾问岗位要求

    岗位职责: 1.负责SAP系统各模块日常运维工作,解决用户在系统操作过程中遇到的问题: 2.评估用户需求(新需求.功能优化)的可实现性,完成SAP系统及相关系统的配置调整及功能实现: 3.负责CRM等 ...

  3. [转]能用HTML/CSS解决的问题就不要使用JS

    原文链接:http://www.codeceo.com/article/html-css-not-js.html 为什么说能使用html/css解决的问题就不要使用JS呢?两个字,因为简单.简单就意味 ...

  4. python拉格朗日插值

    #拉格朗日插值代码 import pandas as pd #导入数据分析库Pandas from scipy.interpolate import lagrange #导入拉格朗日插值函数 inpu ...

  5. 解决问题的方法=>现象-->原因-->方案-->方案的优缺点

    解决问题的方法=>现象-->原因-->方案-->方案的优缺点

  6. html 背景

    用語設置背景的屬性 bgcolor設置背景顏色.可以使用名字.16進制和rgb三種形式的參數: <body bgcolor="#000000"> <body bg ...

  7. BZOJ1906树上的蚂蚁&BZOJ3700发展城市——RMQ求LCA+树链的交

    题目描述 众所周知,Hzwer学长是一名高富帅,他打算投入巨资发展一些小城市. Hzwer打算在城市中开N个宾馆,由于Hzwer非常壕,所以宾馆必须建在空中,但是这样就必须建立宾馆之间的连接通道.机智 ...

  8. 机器学习&深度学习资料收集

    缘由 以下博客都是我在学习过程中看到的一些知识讲解非常好的博文,就不转载了,直接给出链接方便以后重复访问.有了自己的理解之后再重新整理资料发布吧 : ) sklearn系列 http://www.cn ...

  9. [IOI2005]River 河流

    题目大意: 给定n个点的有根树,每条边有边权,每个点有点权w, 你要在k个点上建立伐木场,对于每个没有建伐木场的点x,令与它最近的祖先.有伐木场的点,为y,你需要支付dis(x,y)*w[x]的代价. ...

  10. NOIP 普及组 2014 比例简化

    传送门 https://www.cnblogs.com/violet-acmer/p/9898636.html 题解: 一开始想多了,以为得保证两者之间的相对比率,至少不能改变的太离谱啊. but,直 ...