使用Spring Initializr快速建立Spring Boot項目
Spring Initializr有幾種用法:
- 通過網頁使用
- 通過Spring Tool Suite使用
- 通過IntelliJ IDEA 使用
- 通過Spring Boot CLI使用
在這裡我以IntelliJ IDEA為例:
1、建立一個空項目(也可以直接建立一個Spring Boot的項目不需要建立空項目)
2、然後建立一個新的Module
在建立項目或者子產品的時候若沒有Spring Assistant或者Spring Initializr這個選項則需要在Settings-Plugins中安裝一個元件搜尋spring boot進行安裝,若搜尋不到spring initializr則搜尋Spring assistant進行安裝,安裝完成後重新idea後在建立項目或者子產品的時候就會有這個選項了。
下一步後選擇隻需要在web中勾選Spring web Starter就可以了
建立後的項目結構:
以及預設pom:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springclouddemo</groupId>
<artifactId>initializrdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>initializrdemo</name>
<description>快速建立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-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>
3、我們建立一個controller作為示例
建立一個controller包,在其中建立一個DemoController.java
4、啟動Spring Boot項目
運作InitializrdemoApplication的main方法就可以啟動這個Spring Boot應用了。
以下是Spring Boot的預設啟動輸出,我們可以看到該應用預設以8080端口啟動,使用的Tomcat容器。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.6.RELEASE)
2019-07-02 13:07:56.982 INFO 10040 --- [ main] c.s.i.InitializrdemoApplication : Starting InitializrdemoApplication on HCJ with PID 10040 (E:\IdeaProject\SpringCloudDemo\initializrdemo\target\classes started by Administrator in E:\IdeaProject\SpringCloudDemo)
2019-07-02 13:07:56.982 INFO 10040 --- [ main] c.s.i.InitializrdemoApplication : No active profile set, falling back to default profiles: default
2019-07-02 13:07:58.629 INFO 10040 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-07-02 13:07:58.662 INFO 10040 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-07-02 13:07:58.662 INFO 10040 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-07-02 13:07:58.783 INFO 10040 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-07-02 13:07:58.783 INFO 10040 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1728 ms
2019-07-02 13:07:58.993 INFO 10040 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-07-02 13:07:59.221 INFO 10040 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-07-02 13:07:59.238 INFO 10040 --- [ main] c.s.i.InitializrdemoApplication : Started InitializrdemoApplication in 2.667 seconds (JVM running for 3.445)
5、通過浏覽器通路接口,進行測試:
由此使用Spring Initializr快速搭建的web應用就完成了。
在Spring Boot中預設內建了很多架構,如Spring、Spring web、Spring webmvc、log4j、slf4j等等。
如果我們需要配置一些屬性,比如啟動端口号等則通過resources/application.properties進行配置。