寫在開頭:雖然工作了,但都是業務邏輯,瞭解業務的基礎上碼磚頭就好了,但是框架配置,設計、架構等方面還是得靠自己去充電。本來準備買慕課上的實戰網課進行學習,後來還是感覺先把基礎打牢再開始實戰印象纔是最深的。本次學習筆記都來自方誌鵬老師的博客 https://www.fangzhipeng.com/springboot/2017/05/01/springboot1.html
一、簡介
springboot 的設計目的就是簡化開發,開啓了各種自動裝配,你不想寫各種配置文件,引入相關的依賴就能迅速搭建起一個web工程。它採用的是建立生產就緒的應用程序觀點,優先於配置的慣例。配置變簡單、編碼變簡單,部署變簡單,就好比用了IDEA,再也回不到Eclipse一樣。
二、項目初始化
打開Idea-> new Project ->Spring Initializr ->填寫group、artifact ->鉤上web(開啓web功能)->點下一步就行了。如果你是社區版則需要下載spring assistant。
自動創建完的項目結構:
pom依賴:我還點了幾個插件的選項,如不用寫getter,setter方法的lombok工具等,這些配置都是默認幫你搞定的,讓你專注於你的自己的業務開發,而不是各種配置。其中spring-boot-starter-web不僅包含spring-boot-starter,還自動開啓了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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dennis</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Dennis first Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、項目啓動
和ssm框架不同的地方是:
- 你沒有做任何的web.xml配置。
- 你沒有做任何的sping mvc的配置; springboot自動配置。
- 你沒有配置tomcat ;springboot內嵌tomcat.
如果加入看日誌的代碼,在程序啓動的時候,會看到控制檯打出springboot自動諸如注入了40-50個bean.