springboot hello word
1. 介紹
- Springboot是對(duì)springmvc和spring的包裝
- 利用spring ioc容器和aop特性將xml配置弱化
- 使用內(nèi)嵌的tomcat提供http服務(wù)
- 對(duì)pom文件的包裝弃衍,簡(jiǎn)化maven配置
- 提供健康檢查
2. 準(zhǔn)備
- Jdk(在cmd窗口下執(zhí)行java -version)
- Eclipse
- Maven(在cmd下執(zhí)行mvn -version)
- Mavne國(guó)內(nèi)鏡像配置%mavne_home%/conf/setting.xml
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
3. Hello World
3.1 創(chuàng)建工程
創(chuàng)建maven工程,parent為springboot断凶,全部的pom內(nèi)容為
<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>pers.mateng.demo.springboot</groupId>
<artifactId>springboot-demo-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<dependencies>
<!-- web相關(guān)的pom插件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
3.2 創(chuàng)建啟動(dòng)類
創(chuàng)建一個(gè)Class(Application.class)茵乱,包含main函數(shù)睁枕,內(nèi)容如下:
package pers.mateng.demo.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
//啟動(dòng)入口
SpringApplication.run(Application.class, args);
}
}
3.3 開始springmvc RESTful
創(chuàng)建一個(gè)springmvc controlller挎塌,內(nèi)容如下:
package pers.mateng.demo.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RestHttpController {
@RequestMapping(path="/")
public String restGet() {
return "Hello World!";
}
}
3.4 驗(yàn)證
使用eclipse的run as啟動(dòng)Application的main函數(shù)
瀏覽器輸入http://localhost:8080/忌栅,查看返回值
4. 引入配置文件
4.1 創(chuàng)建application.properties
src/main/resouorce目錄下創(chuàng)建application.properties车酣,主要增加一下內(nèi)容:
- 修改http的服務(wù)端口
- 增加日志
application.properties的全部?jī)?nèi)容如下:
#tomcat port
server.port=8888
#服務(wù)名稱(目前無(wú)實(shí)際意義,等接入微服務(wù)才有用)
spring.application.name=springboot-demo-1
#logger config
#寫日志文件存放的目錄
logging.path=${user.dir}/logs
#日志文件的名稱
logging.file=${logging.path}/springboot-demo-1.log
#日志的等級(jí)
logging.level.root=info
logging.level.pers.mateng = debug
4.2 驗(yàn)證
使用eclipse的run as重新啟動(dòng)Application的main函數(shù)
驗(yàn)證端口
使用新端口8888 瀏覽器輸入http://localhost:8888/狂秘,查看返回值
驗(yàn)證日志
在工程的根目錄下即可查看日志文件springboot-demo-1.log