- 通过 start.spring.io 创建工程
- 通过 IDEA 创建工程
??《Spring Boot In Action》 中的例子
建立一个展示阅读列表的应用。不同的用户将读过的书的数据登记进来,每次进到页面都能看到相应的读书记录。
1. 首先登录页面 start.spring.io。
页面大概长这个样子:
点击空框内的链接,会展示更全面的参数选择。【参数填好后】,选择 “Generate Project” 就可以将一个完整的Spring Boot 工程下载下来了。
工程的目录是这样的:
pom.xml 是工程构建的明细、ReadingListApplication.java 是bootstrap类 也是Sping 的首要配置类、application.properties 是Spring Boot 和应用读取特定属性的文件;在test目录下 的ReadinglistApplicationTests.java 的就是最基本的测试类。
BOOTSTRAPING SPRING
类ReadingListApplication充当了两个角色:配置Spring,启动Spring。虽然Spring Boot 负责了几乎所有的配置任务,但是总要有个入口来告诉Spring Boot 开始进行 AUTO-CONFIGURE,在ReadingListApplication.java 中有一些注解就是唤起自动配置:
@SpringBootApplication // 就是这个注解唤起自动配置 public class ReadinglistApplication { public static void main(String[] args) { SpringApplication.run(ReadinglistApplication.class, args); } }
@SpringBootApplication 触发了 Component-Scanning 和 自动配置。实际上这个注解做了其他三个注解做的事儿。
- @Configuration 等价于在XML中配置了Beans。这个是基于Java的配置。
- @ComponentScan。相当于启动了 Component scanning,其他标记@Controller和@Component的类可以在Spring的Context中被发现和注册。
- @EnableAutoConfiguration 。这个注解本身就是Spring Boot 开启自动配置的。
那么问题来了,怎么启动这个应用呢?最简单的办法大概就是 在IDEA中直接右键->Run main(). 当然还有CLI的、war 形式的。这些后续慢慢写。
现在开发一个简单的 ReadingListApplication了。需要定义一个Book类表示读过的书、一个Repository来存储读书记录、一个Controller相应Http请求、一个页面承接用户的操作。其他的就不需要了。那这些代码这么来写就好了。
Book.java
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @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 getReader() { return reader; } public void setReader(String reader) { this.reader = reader; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } 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; } }
ReadingListRepository.java
package readinglist; /** * Created on 17/1/4. */ import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; public interface ReadingListRepository extends JpaRepository<Book, Long> { List<Book> findByReader(String reader); }
ReadingListController.java
package readinglist; /** * Created on 17/1/4. */ import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; 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; @Controller @RequestMapping("/") @ConfigurationProperties(prefix = "amazon") public class ReadingListController { Logger logger = LoggerFactory.getLogger(ReadingListController.class); private ReadingListRepository readingListRepository; private String associateId; @Autowired public ReadingListController(ReadingListRepository readingListRepository) { this.readingListRepository = readingListRepository; } @RequestMapping(value = "/{reader}", method = RequestMethod.GET) public String getReadingList(@PathVariable("reader") String reader, Model model) { List<Book> bookList = readingListRepository.findByReader(reader); if (null != bookList) { model.addAttribute("books", bookList); } logger.warn("Here IS Log Output. AssociateId is {}.",associateId); 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}"; } public void setAssociateId(String associateId) { this.associateId = associateId; } }
readlingList.html
<html xmlns:th="http://www.w3.org/1999/xhtml"> <head> <title>Reading List</title> <link rel="stylesheet" th:href="@{/style.css}" /> </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 have 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><br/> <textarea name="description" cols="80" rows="5"> </textarea><br/> <input type="submit"></input> </form> </body> </html>
启动即可。