欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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寫(xiě)一個(gè)屬于自己的Starter

    教你利用SpringBoot寫(xiě)一個(gè)屬于自己的Starter

    如果我們將可獨(dú)立于業(yè)務(wù)代碼之外的功配置模塊封裝成一個(gè)個(gè)starter,復(fù)用的時(shí)候只需要將其在pom中引用依賴(lài)即可,SpringBoot為我們完成自動(dòng)裝配,簡(jiǎn)直不要太爽,這篇文章主要給大家介紹了關(guān)于如何利用SpringBoot寫(xiě)一個(gè)屬于自己的Starter,需要的朋友可以參考下
    2022-03-03
  • Springboot使用@Valid 和AOP做參數(shù)校驗(yàn)及日志輸出問(wèn)題

    Springboot使用@Valid 和AOP做參數(shù)校驗(yàn)及日志輸出問(wèn)題

    這篇文章主要介紹的Springboot使用@Valid 和AOP做參數(shù)校驗(yàn)及日志輸出問(wèn)題,本文通過(guò)代碼講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-11-11
  • java利用反射實(shí)現(xiàn)動(dòng)態(tài)代理示例

    java利用反射實(shí)現(xiàn)動(dòng)態(tài)代理示例

    這篇文章主要介紹了java利用反射實(shí)現(xiàn)動(dòng)態(tài)代理示例,需要的朋友可以參考下
    2014-04-04
  • 談?wù)凧ava 線(xiàn)程池

    談?wù)凧ava 線(xiàn)程池

    這篇文章主要介紹了Java 線(xiàn)程池的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-08-08
  • java使用CKEditor實(shí)現(xiàn)圖片上傳功能

    java使用CKEditor實(shí)現(xiàn)圖片上傳功能

    這篇文章主要為大家詳細(xì)介紹了java使用CKEditor實(shí)現(xiàn)圖片上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • JNDI,JTA和JMS簡(jiǎn)介

    JNDI,JTA和JMS簡(jiǎn)介

    這篇文章主要介紹了JNDI,JTA和JMS的相關(guān)內(nèi)容,包括中文釋義,概念解釋等,需要的朋友可以了解下。
    2017-09-09
  • Kotlin基礎(chǔ)教程之函數(shù)定義與變量聲明

    Kotlin基礎(chǔ)教程之函數(shù)定義與變量聲明

    這篇文章主要介紹了Kotlin基礎(chǔ)教程之函數(shù)定義與變量聲明的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Redis之GEO存儲(chǔ)地理位置信息的使用

    Redis之GEO存儲(chǔ)地理位置信息的使用

    在外賣(mài)軟件中的附近的美食店鋪、外賣(mài)小哥的距離,打車(chē)軟件附近的車(chē)輛,交友軟件中附近的小姐姐。我們都可以利用redis的GEO地理位置計(jì)算得出。本文就來(lái)詳細(xì)的介紹一下
    2021-10-10
  • Java經(jīng)典算法匯總之順序查找(Sequential Search)

    Java經(jīng)典算法匯總之順序查找(Sequential Search)

    Java查找算法之順序查找說(shuō)明:順序查找適合于存儲(chǔ)結(jié)構(gòu)為順序存儲(chǔ)或鏈接存儲(chǔ)的線(xiàn)性表。 下面我們來(lái)詳細(xì)說(shuō)明下
    2016-04-04
  • 淺析Java中XPath和JsonPath以及SpEL的用法與對(duì)比

    淺析Java中XPath和JsonPath以及SpEL的用法與對(duì)比

    XPath,即XML路徑語(yǔ)言,是一種用于在XML文檔中查找信息的語(yǔ)言,JsonPath是從XPath中發(fā)展而來(lái)的,專(zhuān)門(mén)用于JSON數(shù)據(jù)格式,本文主要來(lái)講講他們的用法與區(qū)別,需要的可以參考下
    2023-11-11

最新評(píng)論