一、SpringBoot簡介
SpringBoot是整個Spring技術棧的整合,來簡化Spring應用開發,約定大於配置,去繁從簡,just run 就能創建一個獨立的,產品級別的應用。
背景:
J2EE笨重的開發、繁多的配置、底下的開發效率、復雜的部署流程、第三方技術集成難度大。
解決:
"Spring全家桶"時代。
Spring Boot ——> J2EE一站式解決方案
Spring Cloud ——>分布式整體解決問題
優點:
-
- 快速創建獨立運行的Spring項目以及與主流框架集成
- 嵌入的Tomcat,無需打包成WAR包
- starters自動依賴與版本控制
- 大量自動配置,簡化開發,也可修改默認值
- 無需配置xml,無代碼生成,開箱即用
- 准生產環境的運行時應用監控
- 與雲計算天然集成
二、微服務
2014年,martin fowler
微服務:一種架構風格。一個應用應該是一組小型服務,可以通過HTTP的方式進行互通;每一個功能元素最終都是一個可替換和獨立升級的軟件單元
單體應用:All in one
三、環境配置
環境約束:
-jdk1.8:Spring Boot 建議jdk1.7以上;java -version java version "1.8.0_131"
-maven3.x:maven3.3以上版本;Apache Maven 3.6.1
查詢方法:
-IDEA
-SpringBoot 1.5.9版本(穩定版本)
一、IDEA配置
四、SpringBoot HelloWorld
一個功能:瀏覽器發送hello請求,服務器接收請求並相應,響應Hello World 字符串;
1、創建一個maven工程;(jar)
2、導入依賴springboot相關的依賴
<!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.13.RELEASE</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
3、編寫一個主程序,啟動springboot應用
類中代碼如下:
package com.ge; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @SpringBootApplication 來標注一個主程序類,說明這是一個Spring Boot應用 */ @SpringBootApplication public class HelloWorldMainApplication { //快捷鍵是psvm + Tab鍵 public static void main(String[] args) { //spring應用啟動起來 SpringApplication.run(HelloWorldMainApplication.class,args); } }
4、編寫相關的Controller、Service
該類中代碼如下
package com.ge.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * @Controller 處理請求 */ @Controller public class HelloController { /** * 接收來自瀏覽器的hello請求 * @return */ @ResponseBody @RequestMapping("/hello") public String hello(){ return "HelloWorld!"; } }
5、運行主程序測試
6、簡化項目部署
可以復制到我的虛擬機里,然后運行測試:(關於連接問題,可以查看這篇博客https://blog.csdn.net/qq_43054210/article/details/100942898)
五、項目探究
1、POM文件
<!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.13.RELEASE</version> </parent>
;
2、導入的依賴
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
spring-boot-starter-web
spring-boot-starter:spring-boot場景啟動器;幫我們導入了web模塊正常運行所依賴的組件
Spring Boot 將所有的功能場景都抽取出來,做成一個個的starter(啟動器),只需要在項目里面引入這些starter相關場景的所有依賴都會導入進來。
要什么功能就導入什么場景啟動器
2、主程序類,主入口類
/** * @SpringBootApplication 來標注一個主程序類,說明這是一個Spring Boot應用 */ @SpringBootApplication public class HelloWorldMainApplication { //快捷鍵是psvm + Tab鍵 public static void main(String[] args) { //spring應用啟動起來 SpringApplication.run(HelloWorldMainApplication.class,args); } }
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Documented
@java.lang.annotation.Inherited
@org.springframework.boot.SpringBootConfiguration
@org.springframework.boot.autoconfigure.EnableAutoConfiguration
@org.springframework.context.annotation.ComponentScan(excludeFilters = {@org.springframework.context.annotation.ComponentScan.Filter(type = org.springframework.context.annotation.FilterType.CUSTOM, classes = {org.springframework.boot.context.TypeExcludeFilter.class}), @org.springframework.context.annotation.ComponentScan.Filter(type = org.springframework.context.annotation.FilterType.CUSTOM, classes = {org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter.class})})
public @interface SpringBootApplication {
@org.springframework.core.annotation.AliasFor(annotation = org.springframework.boot.autoconfigure.EnableAutoConfiguration.class)
java.lang.Class<?>[] exclude() default {};
@org.springframework.core.annotation.AliasFor(annotation = org.springframework.boot.autoconfigure.EnableAutoConfiguration.class)
java.lang.String[] excludeName() default {};
@org.springframework.core.annotation.AliasFor(annotation = org.springframework.context.annotation.ComponentScan.class, attribute = "basePackages")
java.lang.String[] scanBasePackages() default {};
@org.springframework.core.annotation.AliasFor(annotation = org.springframework.context.annotation.ComponentScan.class, attribute = "basePackageClasses")
java.lang.Class<?>[] scanBasePackageClasses() default {};
@SpringBootApplication:SpringBoot應用標注在某個類上,說明這個類時SpringBoot的主配置類,SpringBoot就應該運行這個類的main方法來啟動SpringBoot應用
@SpringBootConfiguration:Spring Boot得配置類;
標注在某個類上,表示這是一個Spring Boot的配置類;
@Configuration:配置類上來注解這個配置
配置類 ----- 配置文件;配置類也是容器的一個組件;@Component
@EnableAutoConfiguration:開啟自動配置功能;以前我們要配置的東西,Spring Boot幫我們自動配置; @EnableAutoConfiguration
告訴SpingBoot開啟自動配置功能;這樣自動配置才能生效
@org.springframework.boot.autoconfigure.AutoConfigurationPackage @org.springframework.context.annotation.Import({org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration {
@AutoConfigurationPackage:自動配置包
@org.springframework.context.annotation.Import({org.springframework.boot.autoconfigure.AutoConfigurationPackages.Registrar.class}):spring的底層注解
@import給容器中導入一個組件;導入的組件由AutoConfigurationPackages.Registrar.class指定
六、使用Spring Initializer 快速創建Spring Boot項目
IDEA都支持使用Spring的項目創建向導快速創建一個Spring Boot項目;
默認生成的Spring Boot項目的特點:
- 主程序已經生成好了,只需要編寫自己的業務邏輯就好了
- resources文件夾目錄結構
- static:保存所有的靜態資源;js css img
- templates:保存所有的模板頁面;(Spring Boot 默認jar 包使用嵌入式的Tomcat,默認不支持JSP頁面);可以使用模板引擎(freemaker,thymeleaf);
- application.properties:Spring Boot應用的配置文件;可以修改默認配置