Springboot注解,SpringBoot基礎入門 01

 2023-10-13 阅读 33 评论 0

摘要:一、SpringBoot基礎 1.1 原有Spring優缺點分析 1.1.1 Spring的優點分析 Spring是Java企業版(Java Enterprise Edition,JEE,也稱J2EE)的輕量級代替品。無需開發重量級的Enterprise JavaBean(EJB),Spring為企業級Java開發提供

一、SpringBoot基礎

1.1 原有Spring優缺點分析

1.1.1 Spring的優點分析

Spring是Java企業版(Java Enterprise Edition,JEE,也稱J2EE)的輕量級代替品。無需開發重量級的Enterprise JavaBean(EJB),Spring為企業級Java開發提供了一種相對簡單的方法,通過依賴注入和面向切面編程,用簡單的Java對象(Plain Old Java Object,POJO)實現了EJB的功能。

1.1.2 Spring的缺點分析

雖然Spring的組件代碼是輕量級的,但它的配置卻是重量級的。一開始,Spring用XML配置,而且是很多XML配置。Spring 2.5引入了基于注解的組件掃描,這消除了大量針對應用程序自身組件的顯式XML配置。Spring 3.0引入了基于Java的配置,這是一種類型安全的可重構配置方式,可以代替XML。

所有這些配置都代表了開發時的損耗。因為在思考Spring特性配置和解決業務問題之間需要進行思維切換,所以編寫配置擠占了編寫應用程序邏輯的時間。和所有框架一樣,Spring實用,但與此同時它要求的回報也不少。

Springboot注解、除此之外,項目的依賴管理也是一件耗時耗力的事情。在環境搭建時,需要分析要導入哪些庫的坐標,而且還需要分析導入與之有依賴關系的其他庫的坐標,一旦選錯了依賴的版本,隨之而來的不兼容問題就會嚴重阻礙項目的開發進度。

1.2 SpringBoot的概述

1.2.1 SpringBoot解決上述Spring的缺點

SpringBoot對上述Spring的缺點進行的改善和優化,基于約定優于配置的思想,可以讓開發人員不必在配置與邏輯業務之間進行思維的切換,全身心的投入到邏輯業務的代碼編寫中,從而大大提高了開發的效率,一定程度上縮短了項目周期。

1.2.2 SpringBoot的特點

  • 為基于Spring的開發提供更快的入門體驗
  • 開箱即用,沒有代碼生成,也無需XML配置。同時也可以修改默認值來滿足特定的需求
  • 提供了一些大型項目中常見的非功能性特性,如嵌入式服務器、安全、指標,健康檢測、外部配置等
  • SpringBoot不是對Spring功能上的增強,而是提供了一種快速使用Spring的方式

1.2.3 SpringBoot的核心功能

  • 起步依賴

    起步依賴本質上是一個Maven項目對象模型(Project Object Model,POM),定義了對其他庫的傳遞依賴,這些東西加在一起即支持某項功能。

    Spring boot,簡單的說,起步依賴就是將具備某種功能的坐標打包到一起,并提供一些默認的功能。

  • 自動配置

    Spring Boot的自動配置是一個運行時(更準確地說,是應用程序啟動時)的過程,考慮了眾多因素,才決定Spring配置應該用哪個,不該用哪個。該過程是Spring自動完成的。

? 注意:起步依賴和自動配置的原理剖析會在第三章《SpringBoot原理分析》進行詳細講解

二、SpringBoot快速入門

2.1 代碼實現

2.1.1 創建Maven工程

java流程?使用idea工具創建一個maven工程,該工程為普通的java工程即可

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-f03u1f9D-1600498695592)(img\1.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-sZ7N4aqU-1600498695595)(img\2.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-H1DDv17H-1600498695596)(img\3.png)]

基礎。[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-fPwTKLDv-1600498695598)(img\4.png)]

2.1.2 添加SpringBoot的起步依賴

SpringBoot要求,項目要繼承SpringBoot的起步依賴spring-boot-starter-parent

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version>
</parent>

SpringBoot要集成SpringMVC進行Controller的開發,所以項目要導入web的啟動依賴

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>

2.1.3 編寫SpringBoot引導類

要通過SpringBoot提供的引導類起步SpringBoot才可以進行訪問

package com.itheima;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class MySpringBootApplication {public static void main(String[] args) {SpringApplication.run(MySpringBootApplication.class);}}

2.1.4 編寫Controller

在引導類MySpringBootApplication同級包或者子級包中創建QuickStartController

package com.itheima.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;@Controller
public class QuickStartController {@RequestMapping("/quick")@ResponseBodypublic String quick(){return "springboot 訪問成功!";}}

2.1.5 測試

執行SpringBoot起步類的主方法,控制臺打印日志如下:

.   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::        (v2.0.1.RELEASE)2018-05-08 14:29:59.714  INFO 5672 --- [           main] com.itheima.MySpringBootApplication      : Starting MySpringBootApplication on DESKTOP-RRUNFUH with PID 5672 (C:\Users\muzimoo\IdeaProjects\IdeaTest\springboot_quick\target\classes started by muzimoo in C:\Users\muzimoo\IdeaProjects\IdeaTest)
... ... ...
o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-08 14:30:03.126  INFO 5672 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-05-08 14:30:03.196  INFO 5672 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-05-08 14:30:03.206  INFO 5672 --- [           main] com.itheima.MySpringBootApplication      : Started MySpringBootApplication in 4.252 seconds (JVM running for 5.583)

通過日志發現,Tomcat started on port(s): 8080 (http) with context path ‘’

tomcat已經起步,端口監聽8080,web應用的虛擬工程名稱為空

打開瀏覽器訪問url地址為:http://localhost:8080/quick

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-aYZBqiZC-1600498695599)(img\5.png)]

2.2 快速入門解析

2.2.2 SpringBoot代碼解析

  • @SpringBootApplication:標注SpringBoot的啟動類,該注解具備多種功能(后面詳細剖析)
  • SpringApplication.run(MySpringBootApplication.class) 代表運行SpringBoot的啟動類,參數為SpringBoot啟動類的字節碼對象

2.2.3 SpringBoot工程熱部署

我們在開發中反復修改類、頁面等資源,每次修改后都是需要重新啟動才生效,這樣每次啟動都很麻煩,浪費了大量的時間,我們可以在修改代碼后不重啟就能生效,在 pom.xml 中添加如下配置就可以實現這樣的功能,我們稱之為熱部署。

<!--熱部署配置-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId>
</dependency>

注意:IDEA進行SpringBoot熱部署失敗原因

出現這種情況,并不是熱部署配置問題,其根本原因是因為Intellij IEDA默認情況下不會自動編譯,需要對IDEA進行自動編譯的設置,如下:

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-B9kaww83-1600498695601)(img\19.png)]

然后 Shift+Ctrl+Alt+/,選擇Registry

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-hwJyvwsP-1600498695602)(img\20.png)]

2.2.4 使用idea快速創建SpringBoot項目

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-8lamS1gD-1600498695603)(img\6.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-sf2wwd6k-1600498695604)(img\7.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-fxPcG1e9-1600498695604)(img\8.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-97hxkRxm-1600498695605)(img\9.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-cZ5CxyLN-1600498695606)(img\10.png)]

通過idea快速創建的SpringBoot項目的pom.xml中已經導入了我們選擇的web的起步依賴的坐標

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.itheima</groupId><artifactId>springboot_quick2</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springboot_quick2</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>9</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

可以使用快速入門的方式創建Controller進行訪問,此處不再贅述

三、SpringBoot原理分析

3.1 起步依賴原理分析

3.1.1 分析spring-boot-starter-parent

按住Ctrl點擊pom.xml中的spring-boot-starter-parent,跳轉到了spring-boot-starter-parent的pom.xml,xml配置如下(只摘抄了部分重點配置):

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.0.1.RELEASE</version><relativePath>../../spring-boot-dependencies</relativePath>
</parent>

按住Ctrl點擊pom.xml中的spring-boot-starter-dependencies,跳轉到了spring-boot-starter-dependencies的pom.xml,xml配置如下(只摘抄了部分重點配置):

<properties><activemq.version>5.15.3</activemq.version><antlr2.version>2.7.7</antlr2.version><appengine-sdk.version>1.9.63</appengine-sdk.version><artemis.version>2.4.0</artemis.version><aspectj.version>1.8.13</aspectj.version><assertj.version>3.9.1</assertj.version><atomikos.version>4.0.6</atomikos.version><bitronix.version>2.1.4</bitronix.version><build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version><byte-buddy.version>1.7.11</byte-buddy.version>... ... ...
</properties>
<dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot</artifactId><version>2.0.1.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><version>2.0.1.RELEASE</version></dependency>... ... ...</dependencies>
</dependencyManagement>
<build><pluginManagement><plugins><plugin><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-maven-plugin</artifactId><version>${kotlin.version}</version></plugin><plugin><groupId>org.jooq</groupId><artifactId>jooq-codegen-maven</artifactId><version>${jooq.version}</version></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.0.1.RELEASE</version></plugin>... ... ...</plugins></pluginManagement>
</build>

從上面的spring-boot-starter-dependencies的pom.xml中我們可以發現,一部分坐標的版本、依賴管理、插件管理已經定義好,所以我們的SpringBoot工程繼承spring-boot-starter-parent后已經具備版本鎖定等配置了。所以起步依賴的作用就是進行依賴的傳遞。

3.1.2 分析spring-boot-starter-web

按住Ctrl點擊pom.xml中的spring-boot-starter-web,跳轉到了spring-boot-starter-web的pom.xml,xml配置如下(只摘抄了部分重點配置):

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starters</artifactId><version>2.0.1.RELEASE</version></parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.0.1.RELEASE</version><name>Spring Boot Web Starter</name><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>2.0.1.RELEASE</version><scope>compile</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-json</artifactId><version>2.0.1.RELEASE</version><scope>compile</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><version>2.0.1.RELEASE</version><scope>compile</scope></dependency><dependency><groupId>org.hibernate.validator</groupId><artifactId>hibernate-validator</artifactId><version>6.0.9.Final</version><scope>compile</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.0.5.RELEASE</version><scope>compile</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.0.5.RELEASE</version><scope>compile</scope></dependency></dependencies>
</project>

從上面的spring-boot-starter-web的pom.xml中我們可以發現,spring-boot-starter-web就是將web開發要使用的spring-web、spring-webmvc等坐標進行了“打包”,這樣我們的工程只要引入spring-boot-starter-web起步依賴的坐標就可以進行web開發了,同樣體現了依賴傳遞的作用。

3.2 自動配置原理解析

按住Ctrl點擊查看啟動類MySpringBootApplication上的注解@SpringBootApplication

@SpringBootApplication
public class MySpringBootApplication {public static void main(String[] args) {SpringApplication.run(MySpringBootApplication.class);}
}

注解@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 {/*** Exclude specific auto-configuration classes such that they will never be applied.* @return the classes to exclude*/@AliasFor(annotation = EnableAutoConfiguration.class)Class<?>[] exclude() default {};... ... ...}

其中,

@SpringBootConfiguration:等同與@Configuration,既標注該類是Spring的一個配置類

@EnableAutoConfiguration:SpringBoot自動配置功能開啟

按住Ctrl點擊查看注解@EnableAutoConfiguration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {... ... ...
}

其中,@Import(AutoConfigurationImportSelector.class) 導入了AutoConfigurationImportSelector類

按住Ctrl點擊查看AutoConfigurationImportSelector源碼

public String[] selectImports(AnnotationMetadata annotationMetadata) {... ... ...List<String> configurations = getCandidateConfigurations(annotationMetadata,attributes);configurations = removeDuplicates(configurations);Set<String> exclusions = getExclusions(annotationMetadata, attributes);checkExcludedClasses(configurations, exclusions);configurations.removeAll(exclusions);configurations = filter(configurations, autoConfigurationMetadata);fireAutoConfigurationImportEvents(configurations, exclusions);return StringUtils.toStringArray(configurations);
}protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,AnnotationAttributes attributes) {List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());return configurations;
}

其中,SpringFactoriesLoader.loadFactoryNames 方法的作用就是從META-INF/spring.factories文件中讀取指定類對應的類名稱列表

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-0Xe3FkvZ-1600498695607)(img\11.png)]

spring.factories 文件中有關自動配置的配置信息如下:

... ... ...org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\... ... ...

上面配置文件存在大量的以Configuration為結尾的類名稱,這些類就是存有自動配置信息的類,而SpringApplication在獲取這些類名后再加載

我們以ServletWebServerFactoryAutoConfiguration為例來分析源碼:

@Configuration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnClass(ServletRequest.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(ServerProperties.class)
@Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,ServletWebServerFactoryConfiguration.EmbeddedJetty.class,ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })
public class ServletWebServerFactoryAutoConfiguration {... ... ...
}

其中,

@EnableConfigurationProperties(ServerProperties.class) 代表加載ServerProperties服務器配置屬性類

進入ServerProperties.class源碼如下:

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {/*** Server HTTP port.*/private Integer port;/*** Network address to which the server should bind.*/private InetAddress address;... ... ...}

其中,

prefix = “server” 表示SpringBoot配置文件中的前綴,SpringBoot會將配置文件中以server開始的屬性映射到該類的字段中。映射關系如下:

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-92XGYJR6-1600498695608)(img\12.png)]

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/142569.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息