日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

最渣的 Spring Boot 文章

發(fā)布時間:2024/9/21 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最渣的 Spring Boot 文章 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

spring-boot-starter-parent

Maven的用戶可以通過繼承spring-boot-starter-parent項目來獲得一些合理的默認配置。這個parent提供了以下特性:

  • 默認使用Java 8
  • 使用UTF-8編碼
  • 一個引用管理的功能,在dependencies里的部分配置可以不用填寫version信息,這些version信息會從spring-boot-dependencies里得到繼承。
  • 識別過來資源過濾(Sensible resource filtering.)
  • 識別插件的配置(Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).)
  • 能夠識別application.properties和application.yml類型的文件,同時也能支持profile-specific類型的文件(如: application-foo.properties and application-foo.yml,這個功能可以更好的配置不同生產(chǎn)環(huán)境下的配置文件)。
  • maven把默認的占位符${…?}改為了@..@(這點大家還是看下原文自己理解下吧,我個人用的也比較少
    since the default config files accept Spring style placeholders (${…?}) the Maven filtering is changed to use @..@ placeholders (you can override that with a Maven property resource.delimiter).)

starter

啟動器包含一些相應的依賴項, 以及自動配置等.

Auto-configuration

Spring Boot 支持基于Java的配置, 盡管可以將 SpringApplication 與 xml 一起使用, 但是還是建議使用 @Configuration.

可以通過 @Import 注解導入其他配置類, 也可以通過 @ImportResource 注解加載XML配置文件.

Spring Boot 自動配置嘗試根據(jù)您添加的jar依賴項自動配置Spring應用程序. 例如, 如果項目中引入 HSQLDB jar, 并且沒有手動配置任何數(shù)據(jù)庫連接的bean, 則Spring Boot會自動配置內(nèi)存數(shù)據(jù)庫.

您需要將 @EnableAutoConfiguration 或 @SpringBootApplication 其中一個注解添加到您的 @Configuration 類中, 從而選擇進入自動配置.

禁用自動配置

import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.jdbc.*; import org.springframework.context.annotation.*;@Configuration @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public class MyConfiguration { }

如果該類不在classpath中, 你可以使用該注解的excludeName屬性, 并指定全限定名來達到相同效果. 最后, 你可以通過 spring.autoconfigure.exclude 屬性 exclude 多個自動配置項(一個自動配置項集合)

@ComponentScan

SpringBoot在寫啟動類的時候如果不使用 @ComponentScan 指明對象掃描范圍, 默認指掃描當前啟動類所在的包里的對象.

@SpringBootApplication

@Target(value=TYPE)@Retention(value=RUNTIME)@Documented@Inherited@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan(excludeFilters={@ComponentScan.Filter(type=CUSTOM,classes=TypeExcludeFilter.class),}) public @interface SpringBootApplication

使用 @SpringBootApplication 注解相當于使用了下面三個注解.

@EnableAutoConfiguration: 啟用 Spring Boot 的自動配置.
@ComponentScan: 對應用程序所在的包啟用 @Component 掃描.
@Configuration: 允許在上下文中注冊額外的bean或?qū)肫渌渲妙?

ApplicationRunner or CommandLineRunner 區(qū)別

應用服務啟動時,加載一些數(shù)據(jù)和執(zhí)行一些應用的初始化動作。如:刪除臨時文件,清除緩存信息,讀取配置文件信息,數(shù)據(jù)庫連接等。

1、SpringBoot提供了CommandLineRunner接口。當有該接口多個實現(xiàn)類時,提供了@order注解實現(xiàn)自定義執(zhí)行順序,也可以實現(xiàn)Ordered接口來自定義順序。

注意:數(shù)字越小,優(yōu)先級越高,也就是@Order(1)注解的類會在@Order(2)注解的類之前執(zhí)行。

import com.example.studySpringBoot.service.MyMethorClassService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component;@Component @Order(value=1) public class SpringDataInit implements CommandLineRunner {@Autowiredprivate MyMethorClassService myMethorClassService;@Overridepublic void run(String... strings) throws Exception {int result = myMethorClassService.add(8, 56);System.out.println("----------SpringDataInit1---------"+result);} }

2、SpringBoot提供的ApplicationRunner接口也可以滿足該業(yè)務場景。不同點:ApplicationRunner中run方法的參數(shù)為ApplicationArguments,而CommandLineRunner接口中run方法的參數(shù)為String數(shù)組。想要更詳細地獲取命令行參數(shù),那就使用ApplicationRunner接口

import com.example.studySpringBoot.service.MyMethorClassService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.Ordered; import org.springframework.stereotype.Component;@Component public class SpringDataInit3 implements ApplicationRunner,Ordered {@Autowiredprivate MyMethorClassService myMethorClassService;@Overridepublic void run(ApplicationArguments applicationArguments) throws Exception {int result = myMethorClassService.add(10, 82);System.out.println("----------SpringDataInit3---------"+result);}@Overridepublic int getOrder() {return 3;} }

外部化配置

Spring Boot允許你外部化你的配置,這樣你就可以在不同的環(huán)境中使用相同的應用程序代碼,你可以使用 properties 文件、YAML文件、環(huán)境變量和命令行參數(shù)來外部化配置,屬性值可以通過使用 @Value 注解直接注入到你的bean中,通過Spring的 Environment 抽象訪問,或者通過 @ConfigurationProperties 綁定到結構化對象。

@ConfigurationProperties("spring.datasource.username")

Spring Boot使用一種非常特殊的 PropertySource 命令, 該命令旨在允許對值進行合理的覆蓋, 屬性按以下順序考慮:

  • Devtools全局設置屬性在你的主目錄( ~/.spring-boot-devtools.properties 當devtools處于激活狀態(tài)時。
  • 測試中的 @TestPropertySource 注解
  • 測試中的 @SpringBootTest#properties 注解屬性
  • 命令行參數(shù)
  • 來自 SPRING_APPLICATION_JSON(嵌入在環(huán)境變量或系統(tǒng)屬性中的內(nèi)聯(lián)JSON)的屬性
  • ServletConfig 初始化參數(shù)
  • ServletContext 初始化參數(shù)
  • java:comp/env 中的JNDI屬性
  • Java系統(tǒng)屬性(System.getProperties())
  • 操作系統(tǒng)環(huán)境變量
  • 一個只有random.*屬性的RandomValuePropertySource
  • 在你的jar包之外的特殊配置文件的應用程序?qū)傩?#xff08;application-{profile}.properties 和YAML 變體)
  • 在jar中打包的特殊配置文件的應用程序?qū)傩?#xff08;application-{profile}.properties 和YAML 變體)
  • 在你的jar包之外的應用程序?qū)傩?#xff08;application.properties 和YAML 變體)
  • 打包在jar中的應用程序?qū)傩?#xff08;application.properties 和YAML 變體)
  • @PropertySource 注解在你的 @Configuration 類上
  • 默認屬性(通過設置 SpringApplication.setDefaultProperties 指定)

訪問命令行屬性

在默認情況下, SpringApplication 會轉(zhuǎn)換任何命令行選項參數(shù) (也就是說,參數(shù)從 -- 開始, 像 --server.port=9000) 到一個 property, 并將它們添加到Spring Environment 中, 如前所述, 命令行屬性總是優(yōu)先于其他屬性源.

如果不希望將命令行屬性添加到 Environment 中, 你可以使用 SpringApplication.setAddCommandLineProperties(false) 禁用它們.

應用程序?qū)傩晕募?/h3>

SpringApplication 在以下位置從 application.properties 文件加載屬性并將它們添加到Spring Environment :

  • 當前目錄子目錄的 /config
  • 當前目錄
  • 類路徑下 /config 包
  • 類路徑的根目錄

列表按優(yōu)先順序排序(在列表中較高的位置定義的屬性覆蓋在較低位置定義的屬性).

特殊配置文件的屬性

我們可能在不同環(huán)境下使用不同的配置, 這些配置有可能是在同一個文件中或不同文件中.

1.在相同文件中

##################################### Determime which configuration be used spring: profiles: active: "dev"# Mysql connection configuration(share)datasource: platform: "mysql"driverClassName: "com.mysql.cj.jdbc.Driver"max-active: 50max-idle: 6min-idle: 2initial-size: 6--- ##################################### for dev environment spring: profiles: "dev"datasource: # mysql connection user(dev)username: "root"# mysql connection password(dev)password: "r9DjsniiG;>7" --- ##################################### for product environment spring: profiles: "product"datasource: # mysql connection user(product)username: "root"# mysql connection password(product)password: "root" --- ##################################### for test environment spring: profiles: "test"datasource: # mysql connection user(test)username: "root"# mysql connection password(test)password: "root"

這樣在配置完相同屬性的時, 還可以對不同的環(huán)境進行不同的配置.

2.多個配置文件.

我們可以把特定環(huán)境的配置, 放入多個配置文件中, 但是要按照 application-{profile}.properties 格式. 如下圖.

spring.profiles.active 屬性進行設置.

我們也可以把配置文件放在 jar 外面, 使用 spring.config.location 屬性進行設置.

java -jar beetltest-0.0.1-SNAPSHOT.jar -spring.config.location=classpath:/application.properties

總結

以上是生活随笔為你收集整理的最渣的 Spring Boot 文章的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。