1、springBoot 簡介
簡化Spring應用開發
整個spring技術堆疊的大整合
J2EE開發的一個一站式解決方案
2、微服務
微服務一種架構風格
一個應用應該是一組小型服務
可以通過HTTP的方式進行溝通
單體應用:ALL IN ONE
說明:scale ——擴展
每一個功能元素最終都有一個課獨立替換、可獨立升級的軟體單元
springBoot shiyong spring Cloud 來互聯互相呼叫
3、環境準備
-jdk1.8
-idea
-maven
-SpringBoot 1.5.9版本及以上 需要使用jdk1.8版本
1、maven設定
設定maven的setting檔案 添加變異設定 jdk1.8
2、Idea 整合maven
4、SpringBoot
1、創建maven 專案
2、匯入springBoot 的依賴
3、撰寫主程式:啟動springboot
Psvm idea 快捷鍵 創建main方法
5、運行主程式測驗
6、簡化部署
達成jar包 spring 官方檔案 11.5 創建一個可執行jar包
Springboot 打成 jar包配置
org.springframework.boot
Spring-boot-maven-plugin
7、hello word 探究
1、pom檔案
Springboot 專案依賴一個父專案
所依賴的父專案 標注了所有相關依賴包的依賴
該 pom檔案 管理了springboot 版本仲裁中心
以后直接匯入jar包 并不需要匯入版本號
說明:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/><!--lookupparentfromrepository-->
</parent>
創建的springboot專案依賴的parent專案,在該專案中還依賴了一個spring-boot-dependencies 專案,pom檔案如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
進入該專案的組態檔中我們可以看到,springBoot 為我們配置好了需要依賴的包版本資訊
說明:若是引入的jar不在 springboot仲裁 那就需要 標注版本號
2、匯入依賴
Spring-boot-start-web
Spring-boot-start
springboot場景啟動器
幫我們匯入 web模塊相關的依
3、springboot 啟動器分類
springBoot將所有的功能場景都抽取出來,做成啟動器,字需要在專案引入這些starter 相關場景所有的依賴匯入進來,只要匯入場景啟動器就可以了。
在 spring-boot-starters專案中我們可以看到以下子專案(都是啟動器)
<module>spring-boot-starter</module>
<module>spring-boot-starter-activemq</module>
<module>spring-boot-starter-amqp</module>
<module>spring-boot-starter-aop</module>
<module>spring-boot-starter-artemis</module>
<module>spring-boot-starter-batch</module>
<module>spring-boot-starter-cache</module>
<module>spring-boot-starter-cloud-connectors</module>
<module>spring-boot-starter-data-cassandra</module>
<module>spring-boot-starter-data-couchbase</module>
<module>spring-boot-starter-data-elasticsearch</module>
<module>spring-boot-starter-data-gemfire</module>
<module>spring-boot-starter-data-jpa</module>
<module>spring-boot-starter-data-ldap</module>
<module>spring-boot-starter-data-mongodb</module>
<module>spring-boot-starter-data-neo4j</module>
<module>spring-boot-starter-data-redis</module>
<module>spring-boot-starter-data-rest</module>
<module>spring-boot-starter-data-solr</module>
<module>spring-boot-starter-freemarker</module>
<module>spring-boot-starter-groovy-templates</module>
<module>spring-boot-starter-hateoas</module>
<module>spring-boot-starter-integration</module>
<module>spring-boot-starter-jdbc</module>
<module>spring-boot-starter-jersey</module>
<module>spring-boot-starter-jetty</module>
<module>spring-boot-starter-jooq</module>
<module>spring-boot-starter-jta-atomikos</module>
<module>spring-boot-starter-jta-bitronix</module>
<module>spring-boot-starter-jta-narayana</module>
<module>spring-boot-starter-logging</module>
<module>spring-boot-starter-log4j2</module>
<module>spring-boot-starter-mail</module>
<module>spring-boot-starter-mobile</module>
<module>spring-boot-starter-mustache</module>
<module>spring-boot-starter-actuator</module>
<module>spring-boot-starter-parent</module>
<module>spring-boot-starter-security</module>
<module>spring-boot-starter-social-facebook</module>
<module>spring-boot-starter-social-twitter</module>
<module>spring-boot-starter-social-linkedin</module>
<module>spring-boot-starter-remote-shell</module>
<module>spring-boot-starter-test</module>
<module>spring-boot-starter-thymeleaf</module>
<module>spring-boot-starter-tomcat</module>
<module>spring-boot-starter-undertow</module>
<module>spring-boot-starter-validation</module>
<module>spring-boot-starter-web</module>
<module>spring-boot-starter-websocket</module>
<module>spring-boot-starter-web-services</module>
8、helloword
主程式類
注解 @SpringBootApplication
說明該類是springboot的主配置類,
該主注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters={@Filter(
type=FilterType.CUSTOM,
classes={TypeExcludeFilter.class}
),@Filter(
type=FilterType.CUSTOM,
classes={AutoConfigurationExcludeFilter.class}
)}
)
public@interfaceSpringBootApplication{
組合注解
@SpringBootConfiguration
該注解是標識該類是springboot配置類
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public@interfaceSpringBootConfiguration{
@Configuration 配置類上標注該注解
配置類——組態檔
Spring 注解
配置類也是容器中的一個組件:@Component
@EnableAutoConfiguration
開啟自動配置功能
以前需要配置的東西,現在spring Boot 幫我們配置
@EnableAutoConfiguration 告訴springboot
AutoConfigurationPackage:自動配置包
@Import({Registrar.class})
spring底層注解 ,給容器中匯入一個組件:匯入的組件由Registrar.class
AutoConfigurationPackage 將主配置類所在包以及所有子包里面的所有組件掃描到spring容器中
@Import({EnableAutoConfigurationImportSelector.class}) 注解 給容器匯入EnableAutoConfigurationImportSelector
該類的父類有個方法selectImports ()
將所有匯入的組件以全雷鳴的方式回傳;這些主鍵就會以犬類名的方式加到容器中;
那么到底匯入了哪些組件:
List<String> configrations 會給容器中匯入非常多的自動配置類(xxxAutoConfiguration):j就是給容器中匯入這個場景所需要的所有組件,并配置好這些組件;
有了自動配置類,就免去了手動配置,和注入功能組件的作業。
那么如何做到自動配置的那?
List<String>configurations=this.getCandidateConfigurations(annotationMetadata,attributes);
這個方法里面說明了問題:
SpringFactoriesLoader.loadFactoryNames()
該方法引數:EnableAutoConfiguration.class,his.beanClassLoader;
該方法邏輯
J2EE 的整體解決方案和自動配置都在:
——————————————————
9、使用 spring Initializer 創建向導 創建一個springBoot專案
10 、springboot 配置
1、組態檔
Application.properties
Aplication.yml
這兩個都可以作為全域組態檔,檔案名是固定的
組態檔的作用:來修改springBoot 的默認值。
2、yml 基本語法
1、K: V :表示一個鍵值對(V前面必須有空格)
"":不會轉移字串里面的特殊字符:特殊字符會做為本身表達的意思
‘’:會轉移特殊字符 例如 ‘小明 \n 信你’ 輸出 ”小明 \n 信你“
屬性和值是大小寫敏感的。
2、值的寫法
字面量:普通的值(數字、字串、布爾)
物件、map(屬性和值)(鍵值對)
陣列(list\set)
用- 值表示陣列中的一個元素
Pets:
- cat
- pig
- xiaoming
行內寫法類似 json 格式
11、組態檔值注入
組態檔:
person:
name:小明
age:18
dog:
name:艾瑞克
age:3
注解配置物體類
importsun.misc.Contended;
/**
*將組態檔中的屬性值映射到物件中
*ConfigurationProperties告訴springBoo將本類中所有的屬性和組態檔中相關的配置進行系結
*只有該組件是容器中的組件,才能使用
*Component加入容器
*/
@Component
@ConfigurationProperties(prefix="person")
匯入組態檔處理器
<!--匯入組態檔處理器,組態檔進行系結就會有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
匯入處理器之后會有yml提示
lastName 和last-name 等效
12、springBoot 配置properties組態檔編碼問題
1、配置物件屬性
person.name=小明
Person.age=18
Person.map.k1=xiaoming
2、亂碼問題
Idea配置編碼是 utf-8
3、配置 @configurationProperties 和@value的區別
configurationProperties 支持松散系結
value 不支持松散語法系結
value 支持spring語法系結(Spel)
configurationProperties 不支持spring語法系結(Spel)
configurationProperties Jsr3030資料校驗
value 不支持
configurationProperties 支持復雜型別注解
value 不支持
如果說,我們只是需要獲取組態檔中的某個值,推薦使用@value
如果說,我們撰寫了一個javaBean來和組態檔映射,那么推薦使用configurationProperties ,一次性全部注入
4、資料校驗
/**
*將組態檔中的屬性值映射到物件中
*ConfigurationProperties告訴springBoo將本類中所有的屬性和組態檔中相關的配置進行系結
*只有該組件是容器中的組件,才能使用
*Component加入容器
*Validated需要校驗
*/
@Component
@Validated
@ConfigurationProperties(prefix="person")
publicclassPerson{
@Email//標識需要是郵箱格式
13、@PropertySource 、@ImportResource 、@Bean
@PropertySource 加載外部組態檔(類路徑下的檔案)
@ImportResource 匯入spring的組態檔,讓組態檔里面的配置內容生效;
@ImportResource 標注主配置類上,在容器啟動的時候回加載該路徑下的檔案
例如加入組件:
2、springBoot 推薦給容器中添加組件的方式;使用全注解的方式
1、寫個配置類
創建配置類并添加注解
@Configration 指明當前類是一個配置類,就是替代之前的spring組態檔的
@Configration
Public class Myconfigration(){
@Bean
Public HelloWorldService helloworldService(){
Return new HelloWorldService ();
}
}
14、組態檔的占位符
1、組態檔中可以使用亂數
亂數:${random.uuid}
2、站位符
${person.nameq:小明}_dog : person.name 如果在組態檔中存在則參考存在值,弱不存在測使用默認值:小明
15、Profile 多環境支持
1、多profile的方式
我們在主組態檔的時候
server:
port:8081
person:
name:小明
age:18
dog:
name:艾瑞克
age:3
#指定激活使用的組態檔
spring:
profiles:
active:uat
---#檔案塊
server:
port:8083
spring:
profiles:dev
---
server:
port:8084
spring:
profiles:uat //指定當前檔案塊屬于哪個環境
---
2、命令列指定環境配置模式
--spring.profiles.active=dev
Idea 配置位置:
優先 idea 命令列引數
jar包啟動的時候 設定spring.profiles.active=dev引數
3、設定虛擬機引數
-Dspring.profiles.active=dev
當存在虛擬機引數的時候優先虛擬機引數
16、組態檔的加載位置
概述:
spring Boot 啟動的時候會掃描一下位置的application.properties 或者application.yml檔案作為spring Boot的默認組態檔
—file:./config/
—file:./
—classpath:/config/
—classpath:/
—以上是按照優先級從高到低的順序,所有位置的檔案都會被加載,搞優先級配置內容會覆寫低優先級配置內容
—我們可以通過配置spring.config.location來該表默認配置
1、優先級加載順序,加載完成之后,會形成組態檔之間的互補。
2、可以通過spring.config.location改變默認配置
該引數只用來專案大號包之后啟動命令使用改配置改變組態檔,改組態檔會和spring專案配置的組態檔一起起作用。
優點:如果專案發布引數配置不對,可以添加外部組態檔的方式修改回來,而不用從新打包發布。
3、外部配置的加載順序
·springBoot支持多種外部配置方式
這些優先級如下:
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config
SpringBoot 可以從以下位置加載組態檔,所有的配置會形成互補配置。
1、命令列引數
2、來自java:comp/env的JNDI屬性
3、java系統屬性(System.getProperties)
4、作業系統環境變數
5、RandomValuePropertySource配置的random.*屬性值
6、jar包外部的application-{profile}.properties或者application-{profile}.yml(帶spring.profile)組態檔的
7、jar包內部的application-{profile}.properties或者application-{profile}.yml(帶spring.profile
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/77610.html
標籤:Java EE
