Spring Boot
是用來簡化Spring
應用的初始化開發過程。
2:特性:
-
創建獨立的應用(
jar|war形式
);-
需要用到
spring-boot-maven-plugin插件
-
-
直接嵌入式
Tomcat,Jetty,Undertow
等Web容器; -
提供固化的
starter
依賴,簡化構建配置; -
提供
條件化自動裝配
Spring或者第三方組件 -
提供運維(
Production-Ready
)特性,如`指標信息(Metrics),健康檢查或者外部化配置;-
Spring Boot Admin(
Spring Boot Web監控平台
)
-
-
提倡無代碼生成,並且不需要XML配置;
3:環境准備
-
Java運行環境: Spring5.0最低版本要求是
Java8
。 -
模塊類庫管理Apache Maven:SpringBoot官方兼容
Maven 3.2或者更高版本
。 -
裝配IDE(基礎開發環境):
Idea
或者Eclipse
或者MyEclipse
。
4:第一個Spring Boot應用
建立springboot-learn項目,並配置公共POM
依賴。
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<lombok.version>1.16.8</lombok.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.8.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<!--提供簡單注解簡化Java代碼開發-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<!--must configuration executable-->
<configuration>
<executable>true</executable>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
建立spring-boot-helloworld項目,POM
依賴
<dependencies>
<!--spring boot web 依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
建立Spring Boot應用啟動主類
啟動項目,spring-boot-starter-web默認嵌入式web容器時Tomcat,默認啟動端口是8080
。並且
spring-boot-starter-web模塊引入了spring-boot-starter-json
模塊,可以使用Restful
風格API設計。
打開瀏覽器,訪問http://localhost:8080/hello 。效果不在演示。