溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring-boot-starter常用依賴模塊有哪些

發(fā)布時間:2021-08-19 14:09:39 來源:億速云 閱讀:129 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)Spring-boot-starter常用依賴模塊有哪些的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

Spring-boot的2大優(yōu)點(diǎn):

1.基于Spring框架的“約定優(yōu)先于配置(COC)”理念以及最佳實(shí)踐之路。

2.針對日常企業(yè)應(yīng)用研發(fā)各種場景的Spring-boot-starter自動配置依賴模塊,且“開箱即用”(約定spring-boot-starter- 作為命名前綴,都位于org.springframenwork.boot包或者命名空間下)。

應(yīng)用日志和spring-boot-starter-logging

常見的日志系統(tǒng)大致有:java.util默認(rèn)提供的日志支持,log4j,log4j2,commons logging,下面的spring-boot-starter-logging也是其中的一種。

maven依賴:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-logging</artifactId>
  </dependency>

springBoot將使用logback作為應(yīng)用日志的框架,程序啟動時,由org.springframework.boot.logging-Logging-Application-Lisetener根據(jù)情況初始化并使用。

如果要想改變springBoot提供的應(yīng)用日志設(shè)定,可以通過一下原則:

遵循logback的約定,在classpath中使用自己定制的logback.xml配置文件。

在文件系統(tǒng)的任意一個位置提供自己的logback.xml配置文件,然后通過logging.config配置項指向這個配置文件然后引用它,例如在application.properties中指定如下的配置:

logging.config=/{some.path.you.defined}/any-logfile-name-I-like.log}

快速web應(yīng)用開發(fā)與spring-boot-starter-web

maven依賴:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

在當(dāng)下項目運(yùn)行mvn spring-boot:run就可以直接啟用一個嵌套了tomcat的web應(yīng)用。

如果沒有提供任何服務(wù)的Cotroller,訪問任何路徑都會返回一個springBoot默認(rèn)的錯誤頁面(Whitelabel error page)。

嵌入式Web容器層面的約定和定制

spring-boot-starter-web默認(rèn)使用嵌套式的Tomcat作為Web容器對外提供HTTP服務(wù),默認(rèn)端口8080對外監(jiān)聽和提供服務(wù)。

我們同樣可以使用 spring-boot-starter-jetty 或者 spring-boot-starter-undertow 作為Web容器。

想改變默認(rèn)的配置端口,可以在application.properties中指定:

server.port = 9000(the port number you want)

類似的配置還有:

server.address
server.ssl.*
server.tomcat.*

如果上訴仍然沒有辦法滿足要求,springBoot支持對嵌入式的Web容器實(shí)例進(jìn)行定制,可以通過向IoC容器中注冊一個EmbeddedServletContainerCustomizer類型的組件來對嵌入式的Web容器進(jìn)行定制

public class UnveilSpringEmbeddedTomcatCustomizer implements EmbeddedServletContainer{
    public void customize(ConfigurableEmbeddedServletContainer container){
      container.setPort(9999);
      container.setContextPath("C\\hello");
              ...
    }
  }

數(shù)據(jù)訪問與spring-boot-starter-jdbc

maven依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
  </dependency>

默認(rèn)情況下,當(dāng)我們沒有配置任何DataSource,SpringBoot會為我們自動配置一個DataSource,這種自動配置的方式一般適用于測試,開發(fā)還是自己配置一個DataSource的實(shí)例比較好。

如果我們的工程只依賴一個數(shù)據(jù)庫,那么,使用DataSource自動配置模塊提供的參數(shù)是最方便的:

spring.datasource.url=jdbc:mysql://{datasource host}:3306/{databaseName}
spring.datasource.username={database username}
spring.datasource.passwd={database passwd}

還會自動配置的有:JdbcTemplate DateSourceTransactionManager等,我們只要在使用的時候注入(@Autowired)就好了

此外,SpringBoot還支持的數(shù)據(jù)庫有spring-boot-data-jpa spring-boot-data-mongodb

spring-boot-starter-aop應(yīng)用及其使用場景

AOP:Aspect Oriented Programming,面向切面編程

maven依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
  </dependency>

spring-boot-starter-aop主要由2部分組成:

1.位于spring-boot-autoconfigure的org.sringframework.boot.autoconfigure.aop.AopAutoConfiguration提供的@Configuration配置類和相應(yīng)的配置項,即下面的2個配置項:

spring.aop.auto=true
spring.aop.proxy-target-class=false

2.spring-boot-starter-aop模塊提供了針對spring-aop aspectjrt 和aspectjweaver的依賴

應(yīng)用安全與spring-boot-starter-security //todo

感謝各位的閱讀!關(guān)于“Spring-boot-starter常用依賴模塊有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI