spring boot簡介
- Spring Boot來簡化Spring應用開發,約定大于配置,去繁從簡,just run就能創建一個獨立的,產品級別的應用
- 背景:
J2EE笨重的開發、繁多的配置、低下的開發效率、復雜的部署流程、第三方技術集成難度大, - 解決:
“Spring全家桶”時代,
Spring Boot ?J2EE一站式解決方案Spring Cloud ?分布式整體解決方案 - 優點:
快速創建獨立運行的Spring專案以及與主流框架集成
使用嵌入式的Servlet容器,應用無需打成WAR包
starters自動依賴與版本控制
大量的自動配置,簡化開發,也可修改默認值
無需配置XML,無代碼生成,開箱即用
準生產環境的運行時應用監控
與云計算的天然集成
微服務
- 2014,Martin fowler
微服務:架構風格(服務微化)
一個應用應該是一組小型服務;可以通過 http的方式進行互通
單體應用: all in ond

微服務:每個功能元素最終都是一個可獨立替換和獨立升級的軟體單元


詳細參照微服務檔案
環境準備
- jdk 1.8
- maven 3.X
- idea:2020
- spring boot 1.5.9.RELEASE
maven設定
- 給maven 的settings.xml組態檔的profiles標簽添加
<profile>
<id>jdk‐1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<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>
idea設定

spring boot-hello word
- 一個功能:
瀏覽器發送hello請求,服務器接受請求并處理,回應Hello World字串;
創建一個 maven工程;jar

匯入 spring boot相關的依賴
<!--匯入 springboot 父專案-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<!--spring-boot-starter :springboot場景啟動器,幫我們匯入了web模塊正常運行所依賴的 jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--這個插件,可用將應用打包成一個可執行的 jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
撰寫一個主程式;啟動 spring boot應用
/**
* @SpringBootApplication 來標注一個主程式類, 說明這是一個Spring Boot應用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//Spring 應用啟動起來
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}
撰寫相關的 Controller,Service
//@ResponseBody //這個類的所有方法回傳的資料寫給瀏覽器.(如果是 物件轉為 json資料)
//@RestController //RestController 就是 controller和 responseBody的合體
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "Hello World!";
}
}
運行主程式測驗
hello word探究
pom檔案
父專案
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
他的父專案是:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
他來真正管理Spring Boot應用里面的所有依賴版本;
- Spring Boot的版本仲裁中心;
以后我們匯入依賴默認是不需要寫版本;(沒有在dependencies里面管理的依賴自然需要宣告版本號)
啟動器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
</dependency>
- spring-boot-starter-web:
spring-boot-starter:spring-boot場景啟動器;幫我們匯入了web模塊正常運行所依賴的組件;
Spring Boot將所有的功能場景都抽取出來,做成一個個的starters(啟動器),只需要在專案里面引入這些starter
相關場景的所有依賴都會匯入進來,要用什么功能就匯入什么場景的啟動器
主程式類,主入口類
/**
* @SpringBootApplication 來標注一個主程式類,說明這是一個Spring Boot應用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
// Spring應用啟動起來
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
- @SpringBootApplication: Spring Boot應用標注在某個類上說明這個類是SpringBoot的主配置類,SpringBoot就應該運行這個類的main方法來啟動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 @interface SpringBootApplication {- @SpringBootConfiguration
Spring Boot的配置類;標注在某個類上,表示這是一個Spring Boot的配置類;@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration //spring底層注解 @Component public @interface SpringBootConfiguration {- @Configuration: 配置類上標注這個注解;配置類 ----> 組態檔;配置類也是容器中的一個組件
- @EnableAutoConfiguration:開啟自動配置功能;
以前我們需要配置的東西,Spring Boot幫我們自動配置;@EnableAutoConfiguration告訴SpringBoot開啟自動配置功能;這樣自動配置才能生效;@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import({EnableAutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration {- @AutoConfigurationPackage:自動配置包;將主配置類(@SpringBootApplication注解的類)的所在的包及下面所有子包里面的所有組件掃描到 Spring容器
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import({Registrar.class}) public @interface AutoConfigurationPackage {- @Import : spring底層注解@Import,給容器中匯入一個組件;匯入的組件由 Registrar.class決定


- @Import : spring底層注解@Import,給容器中匯入一個組件;匯入的組件由 Registrar.class決定
- @Import({EnableAutoConfigurationImportSelector.class}):
給容器中匯入組件?匯入那些組件的選擇器;將所有需要匯入的組件以全類名的方式回傳;這些組件就會被添加到容器中;會給容器中匯入非常多的自動配置類(xxxAutoConfiguration);就是給容器匯入這個場景需要的所有組件并配置好這些組件;有了這些自動配置類,免去了我們手動撰寫注入功能組件等作業 - EnableAutoConfigurationImportSelector的父類 AutoConfigurationImportSelector的 selectImports方法

- 這些自動配置類怎么就掃描這些?怎么得到的吶
主要呼叫了 SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,ClassLoader);作用是,從類路徑下 META-INF/spring.factories中獲取 EnableAutoConfiguration獲取指定的值//獲取后選的組態檔 protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { //主要呼叫了loadFactoryNames() List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()); Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct."); return configurations; } protected Class<?> getSpringFactoriesLoaderFactoryClass() { return EnableAutoConfiguration.class; } protected ClassLoader getBeanClassLoader() { return this.beanClassLoader; } public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) { String factoryClassName = factoryClass.getName(); try { Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories"); ArrayList result = new ArrayList(); while(urls.hasMoreElements()) { URL url = (URL)urls.nextElement(); Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url)); String factoryClassNames = properties.getProperty(factoryClassName); result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames))); } return result; } catch (IOException var8) { throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() + "] factories from location [" + "META-INF/spring.factories" + "]", var8); } }
Spring Boot在啟動的時候從類路徑下的META-INF/spring.factories中獲取EnableAutoConfiguration指定的值,將這些值作為自動配置類匯入到容器中,自動配置類就生效,幫我們進行自動配置作業;以前我們需要自己配置的東
西,自動配置類都幫我們
j2ee 的整體整合解決方案和自動配置都在 spring-boot-autoconfigure-1.5.9.RELEASE.jar;
- @AutoConfigurationPackage:自動配置包;將主配置類(@SpringBootApplication注解的類)的所在的包及下面所有子包里面的所有組件掃描到 Spring容器
- @SpringBootConfiguration
使用 spring initialize快速創建 Spring boot專案





- idea都支持使用 Spring的專案創建向導快速創建一個 Spring Boot專案
選擇我們生成的 Spring Boot專案- 主程式已經生成好了,我們只需要撰寫位元組的邏輯
- resource檔案夾中目錄結構
- static:保存靜態資源;js,css,images
- templates:保存所有模板頁面;(Spring Boot默認jar包嵌入式的 tomcat,默認不支持 jsp頁面);可以使用模板引擎(freemarker,thymeleaf)
- application.properties: Spring Boot應用的組態檔;可以修改一些默認配置
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/302890.html
標籤:Java
