从一点一滴,学习spring boot。开发工具:jdk1.8、IntelliJ IDEA、maven
项目搭建
这里我打算用一个maven Module目做开发,这样方便以后扩展还有各种例子的演示。查看如何创建maven module project,点击这里!,最好在本机配置一个Nexus
,这样方便maven包的管理和源码的管理
创建好项目后的大体结构如下图:

spring boot maven配置
- 首先在maven父项目(spring-boot-demo)中定义我们要引入的spring-boot的版本和构建jar包插件
- 然后在子项目中(spring-boot-web)中定义引入一些web项目需要用到的jar包,比如tomcat等…
这样做的好处主要是:各模块减少不必要的包引用,模块引用清晰明了。
在spring-boot-demo
的pom.xml
文件中加入:
<!-- 引入spring-boot,定义版本号 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<build>
<!-- 构建JAR文件 -->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
在spring-boot-web
的pom.xml
文件中加入:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 页面渲染组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 代码自动加载组件,实现热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
spring boot的启动
spring boot是集成web容器的,所以直接可以通过run启动,我们只需要写一个启动加载类即可。
新建Application
类,内容如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
/**
* User: R&M www.rmworking.com/blog
* Date: 2017/8/14
* Time: 18:13
* spring-boot-demo
* PACKAGE_NAME
*/
@ComponentScan("org.springboot.*")
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class , args);
}
}
spring boot默认web容器是Tomcat,所以我们还需要配置一下Tomcat的端口号,在resources目录下新建application.properties
配置文件,spring boot会自动读取该配置文件,配置文件内容如下:
#端口号
server.port=7777
#模板引擎不缓存内容
spring.thymeleaf.cache=false
spring.groovy.template.cache=false
启动!!!

控制台打印信息如下:

集成spring mvc 和 页面模板Thymeleaf
Controller
package org.springboot.controller;
import org.springboot.model.PlatformUser;
import org.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Map;
/**
* User: R&M www.rmworking.com/blog
* Date: 2017/8/14
* Time: 18:14
* spring-boot-demo
* org.springboot.controller
*/
@Controller
@RequestMapping("/")
public class PageController {
//注入service
@Autowired private UserService userService;
@RequestMapping(value = "userInfo/{uid}" , method = RequestMethod.GET)
public String userInfo(@PathVariable Integer uid , Map<String ,Object> model){
PlatformUser user = userService.getUserInfo(1);
model.put("user" , user);
return "user";
}
}
Service
package org.springboot.service;
import org.springboot.model.PlatformUser;
import org.springframework.stereotype.Service;
/**
* User: R&M www.rmworking.com/blog
* Date: 2017/8/14
* Time: 18:22
* spring-boot-demo
* org.springboot.service
*/
@Service("userService")
public class UserService {
public PlatformUser getUserInfo(Integer uid){
PlatformUser user = new PlatformUser();
user.setLogonid("admin");
user.setMobile("13123123123");
return user;
}
}
user.html 页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>用户详情</title>
<link rel="stylesheet" th:href="@{/css/style.css}" />
</head>
<body>
<div>
<span th:text="'联系人姓名:' + ${user.logonid}">联系人姓名</span>
</div>
<div>
<span th:text="'联系人电话:' + ${user.mobile}">联系人电话</span>
</div>
</body>
</html>
尾巴
一个简单的sporingboot项目就跑起来了,对比原来的模式spring/springmvc/jsp这种模式,可以发现:
- spring boot基本是0配置
- spring boot不需要单独配置web容器
但是,到这里,如果你的项目正常跑起来,你会发现两个问题:
- 页面中 ${} 符号中的内容,ideea会报错,下边有红色线。
- 如果我们在html文件中改一点东西,不重启无法生效。
转载请注明:R&M » spring-boot,high起来(二) —— 第一个web项目