一、 springboot總覽
1.springboot特性
- 獨立的spring應用
springboot可以以jar包的形式獨立運行,使用java -jar xxx.jar
就可以成功運行項目, - 內嵌servlet容器
使我們在應用項目的主程序中運行main函數即可快速運行。 - 內嵌web容器
直接嵌入tomcat、jetty等web容器(不需要部署war文件) - 提供固話的starter依賴
簡化maven配置,使常見的依賴聚集在一起,形成單條依賴 - 組件自動裝配
Spring Boot會根據我們項目中類路徑的jar包/類,為jar包的類進行自動配置Bean,大大簡化了配置 - 應用監控
springboot提供了基於HTTP、ssh、telnet對運行時的項目進行監控。 - 不需要配置xml
可以完全不使用xml配置,只需要自動配置和Java config
2.准備運行環境
- JDK1.8
- MAVEN
二、理解獨立的spring應用
1.應用類型
- 非web應用 : 主要用於服務提供、調度任務、消息處理
- web應用 : 內嵌servlet或web容器,對外提供HTTP服務
2.@RestController
- @RestController注解用作類的請求控制
- @RequestMapping注解用作方法的請求映射
- @ResponseBody注解用作方法的返回對象映射
- 當有@RestController注解時,不需要添加@ResponseBody注解,可以認為@RestController= @Controller + @ResponseBody
3.官網創建springboot應用
4.基礎的start依賴
- spring-boot-starter-parent : srpingboot的父級依賴
- 默認使用java8,也可手動添加指定版本
<properties> <java.version>1.8</java.version> </properties>
- 默認使用UTF-8編碼,可手動添加配置修改
<properties> <project.build.sourceEncoding>GBK</project.build.sourceEncoding> </properties>
- 省略version信息,可不指定version
- 識別插件配置
比如 exec plugin, surefire
能夠識別 application.properties 和 application.yml 類型的文件
5.springboot打包
- 構建jar文件前提,需要在spring-boot-maven-plugin到pom.xml中
- Spring Boot Maven plugin能夠將Spring Boot應用打包為可執行的jar或war文件當運行“mvn package”進行打包時,會打包成一個可以直接運行的 JAR 文件,使用“java -jar”命令就可以直接運行
- 可以在POM中,指定生成 的是Jar還是War
jar 默認為jar - spring-boot-maven-plugin的命令
- spring-boot:repackage,默認goal。在mvn package之后,再次打包可執行的jar/war
- spring-boot:run,運行Spring Boot應用,與java -jar xxx.jar命令無異
6.springboot的jar文件
- springboot的fat jar文件除了包含傳統的java jar中的資源外,還包含依賴的jar文件,他是一個獨立歸檔的應用文件
- jdk默認支持文件(file)、http、jar等協議,故jdk內建了對應協議的實現,這些實現類均放在sun.net.www.protocol包下,並且類名必須為Handler,
- FILE:sun.net.www.protocol.file.Handler
- JAR:sun.net.www.protocol.jar.Handler
- HTTP:sun.net.www.protocol.http.Handler
- HTTPS:sun.net.www.protocol.https.Handler
- FTP:sun.net.www.protocol.ftp.Handler
- 以上這些類均為java.net.URLStreamHandler的實現類,如果需要擴展springboot的啟動jar文件,則需要把org.springframework.boot.loader.jar.Handler添加到java.protocol.handler.pkgs中,並覆蓋原sun.net.www.protocol.jar.Handler
三、理解固話的Maven依賴
1.spring-boot-starter-parent與spring-boot-dependencies
- spring-boot-starter-parent繼承於spring-boot-dependencies,也就是說spring-boot-starter-parent的管理jar包的能力源於spring-boot-dependencies
- 如果不想使用spring-boot-starter-parent來實現dependencyManagement(依賴管理),而是通過自己手動指定jar包的版本號,可通過以下配置spring-boot-dependencies來為每個jar包設置依賴
- 如果通過spring-boot-dependencies來管理依賴,那么不能使用property的形式覆蓋原始的依賴項,要達到同樣的效果,需要在dependencyManagement里面的spring-boot-dependencies之前添加依賴的東西
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
四、理解嵌入式Web容器
1. tomcat容器
spring boot 的web應用開發必須使用spring-boot-starter-web,其默認嵌入的servlet容器是Tomcat。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<dependencies>
<!-- TOMCAT -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
嵌入的servlet容器版本在pom的以下父依賴項中定義,比如上面的version1.4.3引入了Tomcat版本8.5.6。
如果想改變tomcat版本,也可以更改pom.xml或application.properties文件中的屬性進行修改
- application.properties 文件修改:
<properties>
<tomcat.version>8.5.6</tomcat.version></properties>
</properties>
- pom.xml文件修改:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
<version>${tomcat.version}</version>
</dependency>
如果想使用其它servlet容器,則需要先移除tomcat容器
<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>
2. jetty作為嵌入式servlet容器
將默認的嵌入式容器tomcat切換至jetty
<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-jetty</artifactId>
</dependency>
3. undertow作為嵌入式servlet容器
<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>
五、理解自動裝配
1. 激活自動化裝配
官網提出激活自動化裝配需要將注解@EnableAutoConfiguration 和 @SpringBootApplicaion,將兩者選其一標注在@Configuration類上,@Configuration聲明被標注為配置類
2. 理解@SpringBootApplicaion注解
@SpringBootApplicaion是一個聚合注解,類似的還有@RestController等
@SpringBootApplicaion被用於激活@EnableAutoConfiguration、@ComponentScan、@Configuration三個注解的特性,可以理解為前者等同包含於三個后者:
- @EnableAutoConfiguration:負責激活SpringBoot自動裝配機制
- @ComponentScan:激活@Component的掃描
- @Configuration:聲明被標注為配置類
其中@SpringBootConfiguration屬於@Configuration的派生注解
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplicaion{
...
}
3. 派生注解@Component
- @Component
- @Configuration
- @SpringBootConfiguration
@Repository、@Service、@Controller均屬於@Component派生注解
- @SpringBootConfiguration
- @Configuration
4. @SpringBootApplicaion屬性別名
@AlisaFor注解能夠將一個或多個注解的屬性‘別名’到某個注解中
@SpringBootApplicaion(scanBasePackages = 'com.song.xxx')
六、理解Production-Ready
七、走向注解驅動編程
1. Spring核心注解場景分類
1.1 模式注解
- @Repository:數據倉庫模式
- @Compoent:通用組件模式
- @Service:服務模式
- @Controller:Web控制器模式
- @Configuration:配置類模式
在applicationContext.xml文件中加一行:<context:component-scan base-package="com.song.xxx"/>后
@Component、@Repository、@Service、@Controller都是將類實例化注入到spring管理器中,名字只是一
個分類,實質作用是一樣的
@Repository用於標注數據訪問組件,即DAO組件
@Component泛指組件,當組件不好歸類的時候,我們可以使用這個注解進行標注。
@Service一般標注在業務接口實現類上,用於標注業務層組件
@Controller用於標注控制層組件
@Configuration標注在類上,相當於把該類作為spring的xml配置文件中的<beans>,
作用為:配置spring容器(應用上下文)
用於定義配置類,可替換xml配置文件,被注解的類內部包含有一個或多個被@Bean注解的方法,這些方法將會被
AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext
類進行掃描,並用於構建bean定義,初始化Spring容器
1.2 裝配注解
- @ImportResource:替換xml元素
- @Import:限定@Autowired依賴注入的范圍
- @ComponentScan:springboot中的@ComponentScan相當於spring配置文件中的context:component-scan
@ComponentScan:掃描知道package下標注Spring模式注解的類,如果不添加此注解,則其他的添加注解的類
不會被springboot掃描到,更不會裝入spring容器中。
1.3 依賴注入注解
- @Autowired:Bean的依賴注入
- @Qualifer:細粒度的@Autowired依賴查找
- @Resource(Java注解):Bean依賴注入
:) @Autowired默認按類型裝配
:) @Autowired默認情況下必須要求依賴對象必須存在,如果要允許null值,可以設置
它的required屬性為false,如:@Autowired(required=false)
:) 如果接口有多個實現類,spring並不知道用哪個實現類,這個時候可以結合@Qualifer注解,
注意@Qualifier注解括號里面的必須是Person接口實現類的類名:@Qualifier("StudentService")
:) @Resource后面沒有任何內容,默認通過name屬性去匹配bean,找不到再按type去匹配
:) @Resource指定了name或者type則根據指定的類型去匹配bean:
@Resource(name = "teacher") / @Resource(type = Student.class)
:) @Resource屬於java注解,@Autowired和@Qualifer屬於spring注解,建議使用@Resource注解,
以減少代碼和Spring之間的耦合。
1.4 Bean定義注解
- @Bean:替換xml中
- @DependsOn:替換xml中
- @Lazy:替換xml中
- @Primary:替換xml中
- @Role:替換xml中
- @Lookup:替換xml中
1.5 其他
- @AliasFor:別名注解屬性,實現復用的目的
- @Indexed:提示spring模式注解的掃描效率
- @Profile:配置化條件裝配
- @Conditional:編程條件裝配
2.spring注解編程模型
2.1 元注解
能申明在其他注解上的注解,例如:@Documented、@Component
2.2 spring模式注解
@Component作為一種由spring容器托管的通用模式組件,任何被@Component標注的組件均為組件掃描的候選對象,類似地,凡是被@Component元標注的注解,如@Service所標注的任何組件,也被視作組件的候選對象。
2.3 spring組合注解
例如:
@TransacrionlService組合了@Transacrion和@Service這兩個注解
@SpringBootApplication既是模式注解,也是組合注解
2.4 spring注解屬性別名和覆蓋
較低層注解能覆蓋其元注解的同名屬性
@Component
|-@Service
|-@TransacrionlService
其中@TransacrionlService可以覆蓋@Service
@Service可以覆蓋@Component