序言
俗话说,万事开头难。那想玩转Java web框架,最先开头的就是搭建Spring框架,Spring以其依赖注入、切面、事务管理等诸多特性,为开发者所青睐。
更进一步,基于spring上封装的SpringBoot,配合maven管理,带来两点便捷之处:
第一,引入jar方便,无需指定版本号——框架的父POM,spring-boot-starter-parent 已经引入常用的基础jar包,且指定版本号,防止jar排除;
第二,省去了繁琐的XML文件配置,比如使用redis+spring时候,我们要写很多redis的XML配置项,在项目启动加载时候实例化redis,但在springBoot中,我们仅仅提供redis的地址。
本文主要介绍如何从0开始搭建springBoot框架,后续会介绍如何在框架内部引入Redis、配置中心、消息中间件、ES 等“重型武器”。
关联文章
本文目录
- 新建springboot工程
- 验证工程
一、新建springboot工程
修改父POM的版本号,重新拉包,即可得到springboot最简版工程
二、验证工程
- 配置application.properties
- 写controller
- 添加页面
- 启动springboot工程
- 浏览器调用
配置application.properties
server.port=8080
spring.freemarker.suffix=.html
spring.freemarker.template-loader-path=classpath:/templates/
写controller
@Controller
public class CompanyController {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public String getCompany(Model model) {
model.addAttribute("age", "12");
model.addAttribute("company", "中债登");
return "hello";
}
}
添加页面hello.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title>这里是hello.html界面</title>
</head>
<body>
<h>欢迎来到springboot的世界</h>
<br>
<p th:text="'页面传送过来的公司名称是: ' + ${company}"/>
</body>
</html>
启动springboot工程
springboot自带tomcat,所以在启动类启动后,服务就已经在本地部署了,端口号是配置文件中指定的8080
浏览器调用