Spring Boot的簡單介紹
Spring Boot 本身并不提供Spring框架的核心特性以及擴展功能,只是用于快速诗箍、敏捷地開發(fā)新一代基于Spring框架的應(yīng)用程序挽唉。也就是說瓶籽,它并不是用來替代Spring的解決方案塑顺,而是和Spring框架緊密結(jié)合用于提升Spring開發(fā)者體驗的工具,同時它集成了大量常用的第三方庫配置(例如Jackson, JDBC, Mongo, Redis, Mail等等)扬绪,Spring Boot應(yīng)用中這些第三方庫幾乎可以零配置的開箱即用(out-of-the-box)裤唠,大部分的Spring Boot應(yīng)用都只需要非常少量的配置代碼,開發(fā)者能夠更加專注于業(yè)務(wù)邏輯
構(gòu)建SpringBoot項目
-
構(gòu)建之前先介紹下我的環(huán)境:
編譯器 :idea2017.1.2
JDK:1.8.0_77
完成上面的步驟就已經(jīng)構(gòu)建了一個Maven項目了墓赴,下面需要創(chuàng)建SpringBoot的配置文件
pom.xml文件:
<?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>
<groupId>Example-SpringBoot-Swagger-Group</groupId>
<artifactId>Example-SpringBoot-Swagger</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
-
配置Resource文件夾下面的文件
- static:文件下面存放的是 css js image 等靜態(tài)資源
- templates:存放的是html文件,這兩個文件都是默認無需配置路徑的就可以直接引用的
- application.properties:文件配置的是SpringBoot工程的相關(guān)信息
- ExampleApplication.java:SpringBoot的啟動類章办,需要放在其他類的外面
application.properties:
//端口
server.port=8080
html滨彻、css等文件格式:
ExampleApplication.java:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by shuai on 2017/5/21.
*/
@SpringBootApplication
public class ExampleApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
}
DemoController.java:
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by shuai on 2017/5/21.
*/
@Controller
@RequestMapping(value = "/api/demo")
public class DemoController {
@RequestMapping(value = "/welcome")
public String demoReturnSuccess(){
return "welcome";
}
}
welcome.html:
<!DOCTYPE html>
<html lang="en">
<head>
<title>歡迎來到SpringBoot</title>
</head>
<body>
<h1>歡迎來到SpringBoot</h1>
</body>
</html>
到此簡單的SpringBoot項目就已經(jīng)創(chuàng)建成功了疮绷,運行 ExampleApplication.java文件,啟動成功后椅贱。
在瀏覽器中輸入:http://localhost:8080/api/demo/welcome 就可以看見 templates 文件下的welcome.html文件內(nèi)容
github地址:Spring Boot 教程懂算、技術(shù)棧计技、示例代碼