最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot深入理解之內(nèi)置web容器及配置的總結(jié)

 更新時(shí)間:2019年03月13日 09:17:13   作者:Super_PF  
今天小編就為大家分享一篇關(guān)于SpringBoot深入理解之內(nèi)置web容器及配置的總結(jié),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

前言

在學(xué)會(huì)基本運(yùn)用SpringBoot同時(shí),想必搭過(guò)SSH、SSM等開(kāi)發(fā)框架的小伙伴都有疑惑,SpringBoot在spring的基礎(chǔ)上做了些什么,使得使用SpringBoot搭建開(kāi)發(fā)框架能如此簡(jiǎn)單,便捷,快速。本系列文章記錄網(wǎng)羅博客、分析源碼、結(jié)合微薄經(jīng)驗(yàn)后的總結(jié),以便日后翻閱自省。

正文

使用SpringBoot時(shí),首先引人注意的便是其啟動(dòng)方式,我們熟知的web項(xiàng)目都是需要部署到服務(wù)容器上,例如tomcat、weblogic、widefly(以前叫JBoss),然后啟動(dòng)web容器真正運(yùn)行我們的系統(tǒng)。而SpringBoot搭建的系統(tǒng)卻是運(yùn)行***Application.class中的main方法啟動(dòng)。這是為什么?

原因是SpringBoot除了高度集成封裝了Spring一系列框架之外,還封裝了web容器,SpringBoot啟動(dòng)時(shí)會(huì)根據(jù)配置啟動(dòng)相應(yīng)的上下文環(huán)境,查看EmbeddedServletContainerAutoConfiguration源碼可知(這里SpringBoot啟動(dòng)過(guò)程會(huì)單獨(dú)總結(jié)分析),如下。

@AutoConfigureOrder(-2147483648)
@Configuration
@ConditionalOnWebApplication
@Import({EmbeddedServletContainerAutoConfiguration.BeanPostProcessorsRegistrar.class})
public class EmbeddedServletContainerAutoConfiguration {
  ...
  ...(中間省略部分)
  @Configuration
  @ConditionalOnClass({Servlet.class, Undertow.class, SslClientAuthMode.class})//Undertow配置判斷
  @ConditionalOnMissingBean(
    value = {EmbeddedServletContainerFactory.class},
    search = SearchStrategy.CURRENT
  )
  public static class EmbeddedUndertow {
    public EmbeddedUndertow() {
    }
    @Bean
    public UndertowEmbeddedServletContainerFactory undertowEmbeddedServletContainerFactory() {
      return new UndertowEmbeddedServletContainerFactory();
    }
  }
  @Configuration
  @ConditionalOnClass({Servlet.class, Server.class, Loader.class, WebAppContext.class})//Jetty配置判斷
  @ConditionalOnMissingBean(
    value = {EmbeddedServletContainerFactory.class},
    search = SearchStrategy.CURRENT
  )
  public static class EmbeddedJetty {
    public EmbeddedJetty() {
    }
    @Bean
    public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
      return new JettyEmbeddedServletContainerFactory();
    }
  }
  @Configuration
  @ConditionalOnClass({Servlet.class, Tomcat.class})//Tomcat配置判斷,默認(rèn)為T(mén)omcat
  @ConditionalOnMissingBean(
    value = {EmbeddedServletContainerFactory.class},
    search = SearchStrategy.CURRENT
  )
  public static class EmbeddedTomcat {
    public EmbeddedTomcat() {
    }
    @Bean
    public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
      return new TomcatEmbeddedServletContainerFactory();
    }
  }
}

該自動(dòng)配置類(lèi)表明SpringBoot支持封裝Tomcat、Jetty和Undertow三種web容器,查看spring-boot-starter-web的pom.xml(如下),其默認(rèn)配置為T(mén)omcat。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starters</artifactId>
    <version>1.5.8.RELEASE</version>
  </parent>
  <artifactId>spring-boot-starter-web</artifactId>
  <name>Spring Boot Web Starter</name>
  <description>Starter for building web, including RESTful, applications using Spring
    MVC. Uses Tomcat as the default embedded container</description>
  <url>http://projects.spring.io/spring-boot/</url>
  <organization>
    <name>Pivotal Software, Inc.</name>
    <url>http://www.spring.io</url>
  </organization>
  <properties>
    <main.basedir>${basedir}/../..</main.basedir>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    ...
    ...

若我們使用其他容器,該如何配置,例如該篇文章Tomcat vs. Jetty vs. Undertow: Comparison of Spring Boot Embedded Servlet Containers詳細(xì)比較了SpringBoot中三種容器的性能、穩(wěn)定性等,結(jié)果證明了Undertow在性能和內(nèi)存使用上是最好的。

顯然,更換內(nèi)置容器,能提高SpringBoot項(xiàng)目的性能,由于SpringBoot插拔式的模塊設(shè)計(jì),配置Undertow只需要兩步,如下。

1.第一步,去除原容器依賴(lài),加入U(xiǎn)ndertow依賴(lài)。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

2.第二步,在application.yml中配置Undertow。

server.undertow.accesslog.dir= # Undertow access log directory.
server.undertow.accesslog.enabled=false # Enable access log.
server.undertow.accesslog.pattern=common # Format pattern for access logs.
server.undertow.accesslog.prefix=access_log. # Log file name prefix.
server.undertow.accesslog.rotate=true # Enable access log rotation.
server.undertow.accesslog.suffix=log # Log file name suffix.
server.undertow.buffer-size= # Size of each buffer in bytes.
server.undertow.buffers-per-region= # Number of buffer per region.
server.undertow.direct-buffers= # Allocate buffers outside the Java heap.
server.undertow.io-threads= # Number of I/O threads to create for the worker.
server.undertow.max-http-post-size=0 # Maximum size in bytes of the HTTP post content.
server.undertow.worker-threads= # Number of worker threads.

其余對(duì)容器的更多配置,調(diào)優(yōu)等等不作介紹,可以自行百度Undertow。

到這里,肯定會(huì)有很多人有疑惑,非得用SpringBoot集成的容器作為運(yùn)行環(huán)境嗎?答案是:NO! SpringBoot同樣提供了像往常一樣打war包部署的解決方案。

1.將項(xiàng)目的啟動(dòng)類(lèi)Application.java繼承SpringBootServletInitializer并重寫(xiě)configure方法。

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
  }
  public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
  }
}

2.在pom.xml文件中,< project >標(biāo)簽下面添加war包支持的< package >標(biāo)簽,或者將原標(biāo)簽值jar改成war。

<packaging>war</packaging>

3.在pom.xml文件中,去除tomcat依賴(lài),或者將其標(biāo)記為provided(打包時(shí)排除),provided方式有一點(diǎn)好處是調(diào)試是可以用內(nèi)置tomcat。

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

至此,以上3個(gè)配置便可以完成war方式部署,注意war包部署后訪(fǎng)問(wèn)時(shí)需要加上項(xiàng)目名稱(chēng)。

最后,對(duì)比傳統(tǒng)應(yīng)用容器和springboot容器架構(gòu)圖。

傳統(tǒng)應(yīng)用容器:

springboot容器:

SpringBoot這種設(shè)計(jì)在微服務(wù)架構(gòu)下有明顯的優(yōu)點(diǎn):

  • 可以創(chuàng)建獨(dú)立、自啟動(dòng)的應(yīng)用容器
  • 不需要構(gòu)建War包并發(fā)布到容器中,構(gòu)建和維護(hù)War包、容器的配置和管理也是需要成本和精力的
  • 通過(guò)Maven的定制化標(biāo)簽,可以快速創(chuàng)建SpringBoot的應(yīng)用程序
  • 可以最大化地自動(dòng)化配置Spring,而不需要人工配置各項(xiàng)參數(shù)
  • 提供了產(chǎn)品化特點(diǎn),例如:性能分析、健康檢查和外部化配置
  • 全程沒(méi)有XML配置,也不需要代碼生成

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

  • SpringBoot-application.yml多環(huán)境配置詳解

    SpringBoot-application.yml多環(huán)境配置詳解

    本文主要介紹了SpringBoot-application.yml多環(huán)境配置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • IDEA?Eval?Reset?使用方法匯總

    IDEA?Eval?Reset?使用方法匯總

    本文給大家介紹了IDEA?Eval?Reset?使用方法,安裝插件包括離線(xiàn)安裝方式和在線(xiàn)安裝方式,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2023-10-10
  • Spring?IOC容器Bean注解創(chuàng)建對(duì)象組件掃描

    Spring?IOC容器Bean注解創(chuàng)建對(duì)象組件掃描

    這篇文章主要為大家介紹了Spring?IOC容器Bean注解創(chuàng)建對(duì)象組件掃描,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Springboot中的@ConditionalOnBean注解詳細(xì)解讀

    Springboot中的@ConditionalOnBean注解詳細(xì)解讀

    這篇文章主要介紹了Springboot中的@ConditionalOnBean注解詳細(xì)解讀,@ConditionalOnMissingBean注解兩個(gè)類(lèi),一個(gè)Computer類(lèi),一個(gè)配置類(lèi),想要完成;如果容器中沒(méi)有Computer類(lèi),就注入備用電腦Computer類(lèi),如果有Computer就不注入,需要的朋友可以參考下
    2023-11-11
  • intellij idea14打包apk文件和查看sha1值

    intellij idea14打包apk文件和查看sha1值

    這篇文章主要為大家詳細(xì)介紹了intellij idea14打包apk文件和查看sha1值,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • idea下載svn的項(xiàng)目并且運(yùn)行操作

    idea下載svn的項(xiàng)目并且運(yùn)行操作

    這篇文章主要介紹了idea下載svn的項(xiàng)目并且運(yùn)行操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • Spring中的注解之@Override和@Autowired

    Spring中的注解之@Override和@Autowired

    看別人寫(xiě)的代碼,經(jīng)常會(huì)用到 @Override 和 @Autowired 這兩個(gè)注解.這邊總結(jié)一下這兩個(gè)注解的作用,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • JavaSwing BorderLayout 邊界布局的實(shí)現(xiàn)代碼

    JavaSwing BorderLayout 邊界布局的實(shí)現(xiàn)代碼

    這篇文章主要介紹了JavaSwing BorderLayout 邊界布局的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Java Socket實(shí)現(xiàn)文件傳輸示例代碼

    Java Socket實(shí)現(xiàn)文件傳輸示例代碼

    這篇文章主要介紹了Java Socket實(shí)現(xiàn)文件傳輸示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • 使用Java計(jì)算集合中的組內(nèi)平均值的代碼實(shí)現(xiàn)

    使用Java計(jì)算集合中的組內(nèi)平均值的代碼實(shí)現(xiàn)

    在Java開(kāi)發(fā)中,集合(Collection)是一個(gè)重要的數(shù)據(jù)結(jié)構(gòu),廣泛應(yīng)用于各種場(chǎng)景,計(jì)算集合中的組內(nèi)平均值是一個(gè)常見(jiàn)的操作,本文將深入探討如何使用Java來(lái)計(jì)算集合中的組內(nèi)平均值,涵蓋基本概念、具體實(shí)現(xiàn)、優(yōu)化策略和實(shí)用示例,需要的朋友可以參考下
    2024-06-06

最新評(píng)論

清镇市| 华安县| 黄陵县| 中超| 五寨县| 米易县| 南召县| 昌乐县| 黑龙江省| 牡丹江市| 延庆县| 玉环县| 青岛市| 开阳县| 拉萨市| 大冶市| 永泰县| 无极县| 南涧| 基隆市| 龙里县| 益阳市| 荥经县| 锡林郭勒盟| 河北区| 木兰县| 九江县| 成都市| 汶上县| 彭山县| 富平县| 镇远县| 华亭县| 揭西县| 平安县| 翁源县| 瑞丽市| 绥芬河市| 普兰店市| 大荔县| 闸北区|