《Pro Spring Boot 2》第一章:A Simple Spring Web Application

先来看下手动配置bean的过程(第一章还没使用Spring Boot)

A Simple Spring Web Application

Let’s start by creating a Spring web application— a ToDo app that offers a REST API that can do a CRUD (create, read, update, and delete). To create a new Spring app, you need to have Maven installed. In the following chapters, you can choose either Maven or Gradle.

 

《Pro Spring Boot 2》第一章:A Simple Spring Web Application_第1张图片

 

 《Pro Spring Boot 2》第一章:A Simple Spring Web Application_第2张图片

《Pro Spring Boot 2》第一章:A Simple Spring Web Application_第3张图片

 其中schema.sql内容如下:

create table todo (
  id varchar(36) not null,
  description varchar(255) not null,
  created timestamp,
  modified timestamp,
  completed boolean,
  primary key (id)
);

data.sql内容如下:

insert into todo values ('7fd921cfd2b64dc7b995633e8209f385','Buy Milk','2018-09-23 15:00:01','2018-09-23 15:00:01',false);
insert into todo values ('5820a4c2abe74f409da89217bf905a0c','Read a Book','2018-09-02 16:00:01','2018-09-02 16:00:01',false);
insert into todo values ('a44b6db26aef49e39d1b68622f55c347','Go to Spring One 2018','2018-09-18 12:00:00','2018-09-18 12:00:00',false);

 

 各个java文件见书中详解

 

 Next, let’s configure the Spring context by creating a dispatcherServlet-servlet. xml file. There is a naming convention; if the servlet is named todo in the web.xml file, then the Spring context file should be named todo-servlet.xml. In this case, the servlet was named dispatcherServlet, so it looks for a dispatcherServlet-servlet.xml file (see Listing 1-3).

dispatcherServlet-servlet.xml

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <context:component-scan base-package="com.apress.todo" />

beans>

    

《Pro Spring Boot 2》第一章:A Simple Spring Web Application_第4张图片《Pro Spring Boot 2》第一章:A Simple Spring Web Application_第5张图片

 

 《Pro Spring Boot 2》第一章:A Simple Spring Web Application_第6张图片

 

 

 As you can see, this part requires a little bit of knowledge on how to wire up Spring. If you want to understand more, I recommend several books from the Apress, including Pro Spring 5, by I. Cosmina, et al.

《Pro Spring Boot 2》第一章:A Simple Spring Web Application_第7张图片

 

最终效果:

《Pro Spring Boot 2》第一章:A Simple Spring Web Application_第8张图片

 

 If you click the link, you should have an XML response

可以看到把in-memory数据库中的数据全部都取了出来(由于使用了findAll方法)《Pro Spring Boot 2》第一章:A Simple Spring Web Application_第9张图片

 

 

 So, what do you think of the Spring Framework? Yes, you need to understand what is happening. You need to know how the Spring beans lifecycle works and how the dependency injection is being used. Also, it is important to know a little AOP (aspectoriented programming) because it’s part of the magic of wiring everything to work for us. Do you think it’s too much? Well, if you try to make the same app with a regular Java 2 EE profile, it will be even more work. Remember, it’s not only exposing a REST API, but working with a database, transactions, message converters, view resolvers, and more; that’s why with Spring, web apps are easier to create. But guess what? Spring Boot dos all the boilerplate configuration for you, which speeds up development time by creating enterprise Spring apps!

Summary

There is a lot to learn about the Spring Framework and the role it plays with Spring Boot. A single chapter won’t be enough. So, if you want to know more about it, I encourage you to review the Spring documentation at https://docs.spring.io/spring-framework/ docs/current/spring-framework-reference/. In the next chapter, we start with Spring Boot and learn how easy it is to create the same application we did in this chapter, but “à la Boot.

 

你可能感兴趣的:(《Pro Spring Boot 2》第一章:A Simple Spring Web Application)