1. 程式人生 > 程式設計 >通過idea建立Spring Boot專案並配置啟動過程圖解

通過idea建立Spring Boot專案並配置啟動過程圖解

一、操作步驟

①使用idea新建一個Spring Boot專案

②修改pom.xml

③修改application.properties

④修改編寫一個Hello Spring Boot的Controller

⑤啟動專案訪問

二、詳細步驟

1、File-->New-->Project

2、選擇Spring Initializr 然後Next

3、輸入Artiface 然後Next

4、勾選Web 、模版我們選擇官方推薦的Thymeleaf模版引擎,其他框架、中介軟體、資料庫根據需要選擇即可,而且無需我們手動去新增配置檔案等,選擇完成後Next

選擇模版引擎

5、Finish即可

6、檢視Spring Boot專案結構目錄

7、在pom.xml新增如下內容

注意:如果新建專案時選擇了依賴的mybatis、mongodb之類的啟動時候會報錯,由於沒配置資料來源及mongodb的連線資訊,此時如果只是想測試專案是否搭建成功先註釋即可

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.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-data-mongodb</artifactId>-->
    <!--</dependency>-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--<dependency>-->
      <!--<groupId>org.mybatis.spring.boot</groupId>-->
      <!--<artifactId>mybatis-spring-boot-starter</artifactId>-->
      <!--<version>1.3.2</version>-->
    <!--</dependency>-->
 
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

8、編寫Hello Spring Boot的Controller

package com.example.bootopen.com;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloSpringBootController {
 
  @RequestMapping("/hello")
  public String hello() {
    return "Hello Spring Boot";
  }
}

9、修改配置檔案 application.properties

注意:如果只是簡單測試專案只需要新增埠即可,其他資料來源、快取、靜態資源路徑也可以在此配置。

筆者推薦一種配置檔案模式:另外新建2個配置檔案,一個開發環境,一個線上環境,通過application.properties自由切換

10、啟動專案 選擇Run、Debug啟動

關於@SpringBootApplication註解說明:@SpringBootApplication開啟了Spring的元件掃描和springboot的自動配置功能,相當於將以下三個註解組合在了一起

(1)@Configuration:表名該類使用基於Java的配置,將此類作為配置類

(2)@ComponentScan:啟用註解掃描

(3)@EnableAutoConfiguration:開啟springboot的自動配置功能

訪問專案 http://localhost:8089/hello

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。