1. 程式人生 > >SpringBoot初識

SpringBoot初識

ng- 使用 等等 pack 有一個 ali new file eas

作用

SpringBoot是為了簡化Spring應用的創建、運行、調試、部署等等而出現的,使用它可以專註業務開發,不需要太多的xml的配置。

核心功能

1、內嵌Servlet容器(tomcat、jetty),可以以jar包的方式獨立運行,無需以war包形式部署到獨立的servlet容器中

2、提供很多的starter簡化maven依賴配置

3、自動裝配bean

4、提供使用java配置和註解配置,不建議xml配置

工程創建

使用IDEA:File-->New-->Project-->Spring Initializr,然後兩次Next就可以了

SpringBoot必須使用JDK1.8以上

技術分享圖片

項目結構

技術分享圖片

src/main/java:業務代碼

src/main/resources:配置文件

src/main/resources/static:靜態資源如js、css、圖片、html

src/main/resources/templates:模板文件

src/test/java:測試類

POM文件

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<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>

在創建好的SpringBoot工程中會自動引入兩個starter,但是並沒有定義版本號,這是由於SpringBoot版本號統一由父POM管理,原理和優點與Maven的父pom類似。

spring-boot-starter-parent就是父pom,它只是一個pom文件,並不是真正的jar包

技術分享圖片

但是spring-boot-starter-parent也沒有定義具體的版本號,同時它也有一個父pom:spring-boot-dependencies:

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

而在這個pom文件中則定義了springboot所有starter的版本號:

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.1.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-webflux</artifactId>
                <version>2.1.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-websocket</artifactId>
                <version>2.1.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web-services</artifactId>
                <version>2.1.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>antlr</groupId>
                <artifactId>antlr</artifactId>
                <version>${antlr2.version}</version>
            </dependency>

springboot一個重要的特性就是解決了所有依賴的版本問題,只需引入對應的starter即可。

在官網中我們可以去查找springBoot幫我們定義好的所有starter:有消息組件、AOP、ES、JDBC等等

技術分享圖片

SpringBoot初識