目錄
2 @SpringBootApplication註解是一個組合註解
0 學習資源
視頻課: https://www.bilibili.com/video/av38657363?from=search&seid=15095513327806987593
21天實戰SpringBoot2.9 https://www.bilibili.com/video/av25425463/?spm_id_from=333.788.videocard.1
Reference Guide: https://docs.spring.io/spring-boot/docs/2.1.4.RELEASE/reference/htmlsingle/#using-boot-starter
1 SpringBoot簡介
簡化Spring應用開發的一個框架;整個Spring技術棧的一個大整合;J2EE開發的一站式解決方案
優點
缺點:入門容易,精通難
補充:war/jar/ear包
jar包和war包的區別:
war是一個web模塊,其中需要包括WEB-INF,是可以直接運行的WEB模塊。而jar一般只是包括一些class文件,在聲明瞭Main_class之後是可以用java命令運行的.
它們都是壓縮的包,拿Tomcat來說,將war文件包放置它的\webapps\目錄下,啓動Tomcat,這個包可以自動進行解壓,也就是你的web目錄,相當於發佈了。
war包:通常是做好一個web應用後,通常是網站,打成包部署到容器中。
jar包:通常是開發時要引用通用類,打成包便於存放管理。
ear包:企業級應用,通常是EJB打成ear包。
所有的包都是用jar打的,只不過目標文件的擴展名不一樣。
WAR是Sun提出的一種Web應用程序格式,與JAR類似,也是許多文件的一個壓縮包。這個包中的文件按一定目錄結構來組織:通常其根目錄下包含有Html和Jsp文件或者包含這兩種文件的目錄,另外還會有一個WEB-INF目錄,這個目錄很重要。通常在WEB-INF目錄下有一個web.xml文件和一個classes目錄,web.xml是這個應用的配置文件,而classes目錄下則包含編譯好的Servlet類和Jsp或Servlet所依賴的其它類(如JavaBean)。通常這些所依賴的類也可以打包成JAR放到WEB-INF下的lib目錄下,當然也可以放到系統的CLASSPATH中,但那樣移植和管理起來不方便.
2 微服務介紹
2014 Martin fowler: https://www.martinfowler.com/microservices/ Micro services introduction
微服務:
- 是一種架構風格
- 一個應用程序應該是一組小型服務;可以通過HTTP的方式進行互通
- 每一個功能元素最終都是一個可獨立替換和獨立升級的軟件單元。
單體應用:
微服務vs單體應用
每一個功能元素最終能夠都是一個可以獨立替換和獨立升級的軟件單元
SpringBoot+Spring Cloud+Spring Cloud Data Flowg關係
3 HelloWorld實例
3.1 環境
- jdk 1.8+
- Maven 3.3+
- IntelliJ IDEA 2017
- SpringBoot 1.5.9
3.2 Maven配置: setting.xml
<profile>
<id>jdk-1.8</id>
<activation>
<activateByDefault>true</activateByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
3.3 IDEA設置
3.4 HelloWorld實例編寫
- Step1:創建一個maven工程(.jar)
- Step2:導入springboot相關依賴
- Step3:編寫一個主程序,啓動springboot應用
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*@SpringBootApplication:標註一個主程序類,說明這是一個springboot應用
**/
@SpringBootApplication
public class HelloworldMainApp{
public static void main(String[]args){
//spring應用啓動起來
SpringApplication.run(HelloworldMainApp.class,args);
}
}
- Step4: 編寫相關的controller和service
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController{
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return"helloworld!!!";
}
}
- Step 5: 測試:運行主程序測試
- Step6:部署
- Ideal左側Maven=>lifecycle==> click "package" ==>即可生成jar包=>直接使用jave -jar實行,即使部署地點沒有安裝tomcat也沒事,因爲jar包中已經帶了依賴
4 HelloWorld細節
4.1 pom.xml詳解
- 父項目 - spring-boot-starter-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
- spring-boot-starter-parent的父項目是:spring-boot-dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
在spring-boot-dependencies中:<property>定義了依賴的版本號
spring-boot-dependencies來真正管理springboot中的所有依賴,稱爲springboot的版本仲裁中心。
以後我們導入依賴,默認是不需要寫版本的,沒有在dependency中管理的依賴需要聲明版本
- Starters啓動器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Spring-boot-starter-web: Spring-boot-starter是springboot場景啓動器,幫我們導入了web模塊正常運行所依賴的組件
Springboot將所有的功能場景都抽取出來,做成一個個的starters啓動器,只需要在項目中引入這些starter相關的場景的所有依賴都導入進來,要用什麼功能就導入什麼場景的啓動器
4.2 主程序類,主入口類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*@SpringBootApplication:標註一個主程序類,說明這是一個springboot應用
**/
@SpringBootApplication
public class HelloworldMainApp{
public static void main(String[]args){
//spring應用啓動起來
Spring Application.run(HelloworldMainApp.class,args);
}
}
@SpringBootApplication: 標註在某個類上,說明這個類是springboot的主配置類,springboot就應該運行這個類的main方法來啓動springboot應用。
1 @SpringBootApplication詳解
@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 @interface SpringBootApplication{
……..
}
2 @SpringBootApplication註解是一個組合註解
- @SpringBootConfiguration: springboot的配置類,標註在某個類上,表示這是一個springboot的配置類
- @Configuration: 配置類上來標註這個註解 -- spring底層註解
- 所謂配置類---配置文件,配置類也是容器中的一個組件
- @EnableAutoConfiguration: 開啓自動配置功能
- 需要配置的東西,spring boot可以幫我們配置:告訴springboot開啓自動配置功能,這樣自動配置才能生效
- @AutoConfigurationPackage: 自動配置包
- @Import({Registrar.class}) Spring底層註解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration{
......
}