SpringInitializr快速啟動SpringBoot應用!
Spring Initializrofollow,noindex">http://start.spring.io/ 是引導Spring Boot專案的絕佳工具。
它允許您從非常簡單的UI建立各種基於Spring Boot的應用程式。您可以引導的一些型別的應用程式是:
- Web應用程式
- Restfu的應用程式
- Batch批量處理
Spring Boot提供了大量的入門專案。Spring Initializr支援所有這些以及更多。各種初級專案和支援的選項包括:
- spring-boot-starter-web-services:用於構建公開SOAP Web服務的應用程式
- spring-boot-starter-web - 構建Web應用程式和RESTful應用程式
- spring-boot-starter-test - 編寫出色的單元和整合測試
- spring-boot-starter-jdbc - 傳統的JDBC應用程式
- spring-boot-starter-hateoas - 通過新增HATEOAS功能使您的服務更加RESTful
- spring-boot-starter-security - 使用Spring Security進行身份驗證和授權
- spring-boot-starter-data-jpa - 帶有Hibernate的Spring Data JPA
- spring-boot-starter-cache - 啟用Spring Framework的快取支援
- spring-boot-starter-data-rest - 使用Spring Data REST公開簡單REST服務
在本指南中,我們考慮使用Spring Initializr建立一個簡單的Web應用程式。
使用Spring Initializr建立Web應用程式非常簡單。
如上圖所示,必須執行以下步驟
-
啟動Spring Initializr http://start.spring.io/
並選擇以下內容
- 輸入你的組名:com.xxx.springboot組名
- 選擇xxx-services為腳手架Artifact
-
選擇以下依賴項
- Web
- 如果希望使用資料庫JPA和 Mysql等
- 單擊頁面底部的“生成專案”按鈕。
- 將專案匯入Eclipse或IDea,Idea內部已經集成了這個導航功能。
螢幕截圖顯示了匯入的maven專案的專案結構。
- DemoApplication.java - Spring Boot Launcher。初始化Spring Boot自動配置和Spring應用程式上下文。
- application.properties - 應用程式配置檔案。
- DemoApplicationTests.java - 用於單元測試的簡單啟動器。
- pom.xml - 包含Spring Boot Starter Web的依賴項。使用Spring Boot Starter Parent作為父pom。
讓我們看一下生成的每個檔案
/pom.xml
在pom.xml中配置的三個重要事項。
- Spring Boot Parent Pom - 您可以在這裡閱讀有關Spring Boot Starter Parent的更多資訊 。
- Spring Boot Starter Web - 您可以在此處閱讀有關Spring Boot Starter Web的更多資訊
- Spring Boot Starter外掛
<groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.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>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-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>
程式碼:
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
@SpringBootApplication - 初始化Spring Boot自動配置和Spring應用程式上下文。詳細見這裡SpringApplication.run - 啟動Spring Boot應用程式的靜態方法。
當您將DemoApplication.java作為Java應用程式執行時,將生成以下日誌:
._______ _ _
/\\ / ___'_ __ _ _(_)_ ____ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/___)| |_)| | | | | || (_| |) ) ) )
'|____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::(v1.5.8.RELEASE)
2018-09-19 14:32:29.277INFO 17776 --- [main] com.example.demo.DemoApplication: Starting DemoApplication on banqjdon with PID 17776 (D:\temp\demo\target\classes started by banq in D:\temp\demo)
2018-09-19 14:32:29.279INFO 17776 --- [main] com.example.demo.DemoApplication: No active profile set, falling back to default profiles: default
2018-09-19 14:32:29.331INFO 17776 --- [main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2357d90a: startup date [Wed Sep 19 14:32:29 CST 2018]; root of context hierarchy
恭喜,Web應用已經在http://localhost:8080可啟動了。當然如果需要可訪問,還需要配置REST埠。