前幾天正好在做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
注意:如果只是簡單測試項目只需要添加端口即可,其他數據源、緩存、靜態資源路徑也可以在此配置。
server.port=8089
筆者推薦一種配置文件模式:另外新建2個配置文件,一個開發環境,一個線上環境,通過application.properties自由切換
10、啓動項目 選擇Run、Debug啓動
關於@SpringBootApplication註解說明:@SpringBootApplication開啓了Spring的組件掃描和springboot的自動配置功能,相當於將以下三個註解組合在了一起
(1)@Configuration:表名該類使用基於Java的配置,將此類作爲配置類
(2)@ComponentScan:啓用註解掃描
(3)@EnableAutoConfiguration:開啓springboot的自動配置功能
訪問項目 http://localhost:8089/hello
Spring Boot與Spring Cloud學習使用可參看筆者博客
①Spring Cloud入門教程之服務註冊與發現Eureka
②Spring Cloud入門教程之服務消費者 Ribbon
③Spring Cloud入門教程之服務消費者 Feign
④Spring Cloud入門教程之斷路器 Hystrix
⑤Spring Cloud入門教程之斷路由網關 Zuul
⑥Spring Cloud入門教程之分佈式配置中心 Spring Cloud Config
⑦idea下新建Spring Boot項目並配置啓動
⑧Spring Boot無法自動注入bean問題解決方案
⑨idea 設置Spring Boot熱部署
————————————————
版權聲明:本文爲CSDN博主「zjh_746140129」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/zjh_746140129/java/article/details/80101122