这是《SpringBoot in Action》上的一个应用程序,这里基于Maven而不是Gradle构建工具,主要是一个阅读列表的应用程序,用户输入图书信息,查看列表,删除读过的书
通过IDEA创建一个springboot工程,需要选中依赖,包括如下
Spring MVC处理WEB请求,Thymeleaf定义WEB试图,Spring Data JPA将阅读列表持久化到H2数据库里,通过Maven来构建
首先是ReadingListApplication.java类,应用程序的启动引导类
package com.lihuia.readinglist;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ReadinglistApplication {
public static void main(String[] args) {
SpringApplication.run(ReadinglistApplication.class, args);
}
}
这个类在Spring Boot引用程序里有两个作用:配置和启动引导
1、注解@SpringBootApplication开启了Spring组件扫描和Spring Boot的自动配置功能,它包含了三个注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM,
classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
(1)Spring的@Configuration注解表明这个类使用的Spring是基于Java配置,而不是XML配置
(2)Spring的@ComponentScan注解,组件扫描,IOC里面有说,这样WEB控制器@Controller注解的类和其它组件就能被自动发现并注册为Spring引用上下文中的Bean
(3)SpringBoot的@EnableAutoConfiguration注解,开启自动配置
2、ReadingListApplication是一个启动引导类,main()使得可以在命令行里将这个应用程序当做一个可执行JAR来运行,SpringApplication.run()传递了一个ReadingListApplication类引用来启动
其实此时就可以直接运行这个应用程序,会启动一个默认监听8080端口的Tomcat服务器,但是由于没有Controller类,因此返回404
当然这里的监听端口可以修改,比如讲资源文件application.properties里修改为
server.port=18080
那么启动应用程序监听的端口变成了18080
上面基本就是SpringBoot应用的初始化内容,接下来就是构建应用程序,这里主要是用maven,pom文件如下
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lihuia</groupId>
<artifactId>readinglist</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>readinglist</name>
<description>ReadingList project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
下面的例子基本都是照搬书上的例子
3、定义领域模型,该应用程序是读者阅读列表上的书,因此需要定义一个Book类
@Entity注解表明它是一个JPA实体
@Id注解说明这个字段是实体的唯一标识
@GeneratedValue注解说明这个字段的值自动生成
package com.lihuia.readinglist;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* Copyright (C), 2018-2019
* FileName: Book
* Author: lihui
* Date: 2019/5/26
*/
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String reader;
private String isbn;
private String title;
private String author;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getReader() {
return reader;
}
public void setReader(String reader) {
this.reader = reader;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
4、定义仓库接口
需要定义用于把Book对象持久化到数据库的仓库,因为用了Spring Data JPA,因此需要定义一个接口,扩展Spring Data JPA的JpaRepository接口
通过扩展JpaRepository,ReadingListRepository继承了多个常用持久化操作的方法
JpaRepository是个泛型接口,有两个参数:仓库操作的领域对象类型,以及其ID属性的类型;findByReader()方法可以根据读者的用户名来查找阅读列表
该控制器有两个方法:
readersBooks()处理/{reader}上的HTTP GET请求,根据路径里指定的读者,从(通过控制器的构造方法注入的)仓库获取Book列表,随后将这个列表放入模型,用的键是books,最后返回readingList作为呈现模型的视图逻辑名称
addToReadingList()处理/{reader}上的HTTP POST请求,将请求正文里的数据绑定到一个Book对象上;该方法把Book对象的reader属性设置为读者的姓名,随后通过仓库的save()方法保存修改后的Book对象,最后重定向到/{reader}(控制器中的另一个方法会处理该请求)
package com.lihuia.readinglist;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* Copyright (C), 2018-2019
* FileName: ReadingListRepository
* Author: lihui
* Date: 2019/5/26
*/
public interface ReadingListRepository extends JpaRepository<Book, Long> {
List<Book> findByReader(String reader);
}
5、创建WEB页面
定义好了应用程序的领域模型,把领域对象持久化到数据库里的仓库接口,剩下就是创建WEB前端了,通过Spring MVC控制器为应用程序处理HTTP请求
@Controller注解,组件扫描会自动将其注册为Spring应用程序上下文里的一个Bean
@RequestMapping注解,将其中所有的处理器方法都映射到”/”这个URL路径上
package com.lihuia.readinglist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
/**
* Copyright (C), 2018-2019
* FileName: ReadingListController
* Author: lihui
* Date: 2019/5/26
*/
@Controller
@RequestMapping("/")
public class ReadingListController {
private ReadingListRepository readingListRepository;
@Autowired
public ReadingListController(ReadingListRepository readingListRepository) {
this.readingListRepository = readingListRepository;
}
@RequestMapping(value = "/{reader}", method = RequestMethod.GET)
public String readersBooks(@PathVariable("reader") String reader, Model model) {
List<Book> readingList = readingListRepository.findByReader(reader);
if (readingList != null) {
model.addAttribute("books", readingList);
}
return "readingList";
}
@RequestMapping(value = "/{reader}", method = RequestMethod.POST)
public String addToReadingList(@PathVariable("reader") String reader, Book book) {
book.setReader(reader);
readingListRepository.save(book);
return "redirect:/{reader}";
}
}
6、创建视图
readersBooks()方法最后返回readingList作为逻辑视图名,为此必须创建该视图,用Thymeleaf来定义应用程序的视图,在src/main/resources/templates里创建一个名为readingList.html的文件
这里的模板定义了一个HTML页面,分两部分,上面是读者的阅读列表中的图书清单,下面是一个表单,可以从这里添加新书
<html>
<head>
<title>Reading List</title>
<link rel="stylesheet" th:href="@{/style.css}"></link>
</head>
<body>
<h2>Your Reading List</h2>
<div th:unless="${#lists.isEmpty(books)}">
<dl th:each="book : ${books}">
<dt class="bookHeadline">
<span th:text="${book.title}">Title</span> by
<span th:text="${book.author}">Author</span>
<!--(ISBN: <span th:text=${book.isbn}">ISBN</span>-->
</dt>
<dd class="bookDescription">
<span th:if="${book.description}"
th:text="${book.description}">Description</span>
<span th:if="${book.description eq null}">
No description available
</span>
</dd>
</dl>
</div>
<div th:if="${#lists.isEmpty(books)}">
<p>You hava no books in your book list</p>
</div>
<hr />
<h3>Add a book</h3>
<form method="POST">
<label for="title">Title:</label>
<input type="text" name="title" size="50"></input><br/>
<label for="author">Author:</label>
<input type="text" name="author" size="50"></input><br/>
<label for="isbn">ISBN:</label>
<input type="text" name="isbn" size="15"></input><br/>
<label for="description">Description:</label>
<textarea name="description" cols="80" rows="5"></textarea><br/>
<input type="submit"></input>
</form>
</body>
</html>
7、为了美观,可以添加css样式文件style.css,在src/main/resources/static目录下
body {
background-color: #cccccc;
font-family: Arial,Helvetica,sans-serif;
}
.bookHeadline {
font-size: 12pt;
font-weight: bold;
}
.bookDescription {
font-size: 10pt;
}
label {
font-weight: bold;
}
在IDEA里直接运行SpringBoot程序,打开web页面:http://127.0.0.1:18080/readingList
OVER