zoukankan      html  css  js  c++  java
  • 《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.

     

     其中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>
    
        <!--
        <?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"
               xmlns:mvc="http://www.springframework.org/schema/mvc"
               xmlns:jpa="http://www.springframework.org/schema/data/jpa"
               xmlns:jdbc="http://www.springframework.org/schema/jdbc"
               xmlns:tx="http://www.springframework.org/schema/tx"
               xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
                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
                http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
            <context:component-scan base-package="com.apress.todo" />
    
    
            <mvc:annotation-driven>
                <mvc:message-converters>
                    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                        <property name="objectMapper" ref="jsonMapper"/>
                    </bean>
                    <bean class="org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter">
                        <property name="objectMapper" ref="xmlMapper"/>
                    </bean>
    
                </mvc:message-converters>
            </mvc:annotation-driven>
    
            <bean id="jsonMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                <property name="simpleDateFormat" value="yyyy-MM-dd HH:mm:ss" />
            </bean>
    
    
            <bean id="xmlMapper" parent="jsonMapper">
                <property name="createXmlMapper" value="true"/>
            </bean>
    
            <mvc:resources mapping="/webjars/**" location="classpath:META-INF/resources/webjars/" />
    
            <jpa:repositories base-package="com.apress.todo.repository" />
    
            <jdbc:embedded-database id="dataSource" type="H2">
                <jdbc:script location="classpath:META-INF/sql/schema.sql" />
                <jdbc:script location="classpath:META-INF/sql/data.sql" />
            </jdbc:embedded-database>
    
            <bean id="jpaVendorAdapter"
                  class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
            </bean>
    
    
            <bean id="entityManagerFactory"
                  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                <property name="dataSource" ref="dataSource" />
                <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
            </bean>
    
            <bean id="transactionManager"
                  class="org.springframework.orm.jpa.JpaTransactionManager">
                <property name="entityManagerFactory" ref="entityManagerFactory"/>
            </bean>
    
            <tx:annotation-driven transaction-manager="transactionManager" />
    
            <bean
                    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/views/" />
                <property name="suffix" value=".jsp" />
            </bean>
    
            <bean id="h2WebServer" class="org.h2.tools.Server" factory-method="createWebServer"
                  init-method="start" destroy-method="stop">
                <constructor-arg value="-web,-webAllowOthers,-webDaemon,-webPort,8082" />
            </bean>
            -
    
        </beans>
        -->

     

     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.

    最终效果:

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

    可以看到把in-memory数据库中的数据全部都取了出来(由于使用了findAll方法)

     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.

  • 相关阅读:
    [OpenGL(C)] 旋转立体三角形
    [MSSQL] (命令)列出所有表.字段名.主键.类型.长度.小数位数等信息
    [端口] 端口大全及端口关闭方法
    [网络] IP的划分,超详细
    [C++] 面向对象院校管理系统
    [JSVM2] (开源)JS星际争霸(for JSVM2)
    [MSSQL,MySQL,Oracle] Join用法
    [其它] .NET 世界排名榜
    [C] (回溯法)计算总费用最小费用
    [OpenGL(Win32)] 3D 轮廓字体
  • 原文地址:https://www.cnblogs.com/JasonPeng1/p/12263527.html
Copyright © 2011-2022 走看看