Eclipse 創建Spring boot項目

創建一個maven項目,最終目錄如下


在 src/main/resources 目錄下創建 application.properties 文件、static 和 templates 的文件夾。

application.properties:用於配置項目運行所需的配置數據。

static:用於存放靜態資源,如:css、js、圖片等。

templates:用於存放模板文件。

在 pom.xml 文件中添加如下依賴

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
		<relativePath />
	</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>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

創建Controller類

package com.lzgsea.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
public class TestController {

	@RequestMapping("/boot")
	public String boot() {
		return "Hello Spring Boot";
	}
}

創建springboot啓動類

package com.lzgsea.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootApplication.class, args);
	}
}

啓動項目:

在 SpringbootApplication 類中,右鍵 Run as -> Java Application。當看到 “Tomcat started on port(s): 8080 (http)” 字樣說明啓動成功。

打開瀏覽器訪問 http://localhost:8080/boot/,結果如下:


熱部署配置

當我們修改文件和創建文件時,都需要重新啓動項目。這樣頻繁的操作很浪費時間,配置熱部署可以讓項目自動加載變化的文件,省去的手動操作。

在 pom.xml 文件中添加如下配置:

<!-- 熱部署 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
    <scope>true</scope>
</dependency>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!-- 沒有該配置,devtools 不生效 -->
                <fork>true</fork>
            </configuration>
        </plugin>
    </plugins>
</build>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章