step6----->往工程中添加spring boot项目------->修改pom.xml使得我的project是基于spring boot的,而非直接基于spring framework

文章内容概述:

  spring项目组其实有多个projects,如spring IO platform用于管理external dependencies的版本,通过定义BOM(bill of materials)来管理所引入的external dependencies的版本,从而避免版本冲突问题,再如spring web flow项目,专门为构建流形式的web application提供支持。除了刚刚所叙述的这两个project之外,spring社区还有若干其他project,分别可应用于不同application的开发。Spring协会的这些project都是基于spring framework来创建的,所以支持spring框架的特色功能。同时,它们又对spring framework的modules以及项目可能依赖的三方库、插件等做了基本的封装,在这些project的基础上进行自己的项目的开发,会比直接基于spring framework创建自己的project更便捷。

  我们想要搭建的机器学习管理平台是用于提供restful web services,综合考虑spring社区各个projects的特点,决定舍弃原来的决定,由直接基于the spring framework搭建自己的web网站改为基于spring boot来搭建自己的网站,希望由此简化整个开发过程。

具体操作:

  step1,了解spring boot的特点,参见官网documentation

  step2, 参照  官网guids  以及 git上的sample工程 向项目中引入spring boot

        step2.1 修改pom.xml,包括

              注释掉之前引入的spring framework的modules相关的配置,以及

              添加spring boot相关的配置

            项目最终的pom.xml如下

          • <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/maven-v4_0_0.xsd">
              <modelVersion>4.0.0</modelVersion>
              <groupId>bupt.jinmensuyin.webapp</groupId>
              <artifactId>leapmotion-web</artifactId>
              <packaging>war</packaging>
              <version>0.0.1</version>
              <name>leapmotion-web Maven Webapp</name>
              
              <!--spring boot中定义了pom.xml,
                      该pom.xml中引入了spring framework的若干 modules如spring-core、spring-context等,
                      还引入了三方插件如spring-boot-maven-plugin等
                      还引入了spring framework的external dependencies...
                    所以将spring-boot-starter-parent这个pom.xml作为自己的project的parent POM
                    这样一来,就可以简化自己项目的pom.xml的配置-->
              <parent>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-parent</artifactId>
                  <version>1.4.2.RELEASE</version>
              </parent>
              <dependencies>
                  <!-- 引入spring-boot中的若干模块作为自己的项目的external dependencies -->
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-web</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-test</artifactId>
                      <scope>test</scope>
                  </dependency>
                  <!-- 使用Jackson JSON library完成自己工程中编写的POJO类向JSON字符串的自动转化
                      如下面步骤中,可以将Greeting类实例自动转化成JSON字符串(其实就是自动将该POJO类对象的属性串成JSON格式的字符串) 
                      -->
                  <dependency>
                      <groupId>com.jayway.jsonpath</groupId>
                      <artifactId>json-path</artifactId>
                      <scope>test</scope>
                  </dependency>
                  <!-- 单元测试相关的lib,提供用于单元测试的相关API
                       用于白盒测试,即程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能的情况下编写的测试代码
                       根据Junit所定义的规则进行编写相关测试代码(如测试代码必须继承特定的类等等),可以实现“自动测试”
                       对不同性质的被测对象,如Class,Jsp,Servlet,Ejb等,Junit有不同的使用技巧-->
                <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <scope>test</scope>
                </dependency>
            <!--20161221:lxrm
                修改:将下面的这些external dependencies注释掉
                注释原因:研究了Spring协会旗下的所有projects之后,决定在spring boot这个project的基础上搭建自己的网站,
                而不是直接依赖于spring framework来开发自己的网站,以期能够减少工作量,加快网站搭建进程 ,
                所以将下面的external dependencies注释掉,改为引进spring boot
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aop</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aspects</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-expression</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-orm</artifactId>
                </dependency>
             -->
               
              </dependencies>
              
              <build>
                <finalName>leapmotion-web</finalName>
                <!-- 注释原因:没注释之前,报错,错误原因是parent pom中已经指定过一次该插件,将此处配置注释掉以后不再报错
                    spring-boot-maven-plugin的功能有很多,具体包括:
                        It collects all the jars on the classpath and builds a single, runnable "über-jar",
                     which makes it more convenient to execute and transport your service.
                        It searches for the public static void main() method to flag as a runnable class.
                        It provides a built-in dependency resolver that sets the version number 
                     to match Spring Boot dependencies. You can override any version you wish, 
                     but it will default to Boot’s chosen set of versions.
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                    </plugin>
                </plugins>
                -->
              </build>
              
              <dependencyManagement>
                  <dependencies>
                      <!-- BOM:bill of materials,是一个external dependency的列表,该列表中确定了各个external dependency的版本
                          并且保证各个dependencies之间不会出现版本冲突问题 -->
                      <dependency>
                        <groupId>io.spring.platform</groupId>
                        <artifactId>platform-bom</artifactId>
                        <version>Athens-SR1</version>
                        <type>pom</type>
                        <scope>import</scope>
                    </dependency>
                  </dependencies>
              </dependencyManagement>
              
              <repositories>
                  <repository>
                      <id>spring-releases</id>
                      <url>https://repo.spring.io/libs-release</url>
                  </repository>
              </repositories>
              <pluginRepositories>
                  <pluginRepository>
                      <id>spring-releases</id>
                      <url>https://repo.spring.io/libs-release</url>
                  </pluginRepository>
              </pluginRepositories>
              
            </project>
                    

  step3,编写java程序,测试spring boot是否成功引入

      • 参考教程:
      • 代码:
        •   Greeting.java  POJO类(domain/Model层)
        •   GreetingController.java  (Controller层,有@RequestMapping之类的标注。In Spring’s approach to building RESTful web services, HTTP requests are handled by a controller. These components are easily identified by the @RestController annotation, and the GreetingController below handles GET requests for /greeting by returning a new instance of the Greeting class:)
        •       GreetingConfiguration.java (相当于配置文件 )
        •       GreetingInitializer.java(相当于配置文件:web.xml中所有与spring(springMVC)相关的配置)
      • /**
         * @author lxrm
         * @date 20161221
         * @description:spring boot的入门程序:hello world程序
         * @function:这个类是一个POJO类,属于Model层的对象
         * */
        package hello;
        public class Greeting {
            private final long id;
            private final String content;
            
            public Greeting(long id,String content){
                this.id=id;
                this.content=content;
            }
            
            public long getId(){
                return this.id;
            }
            public Stirng getContent(){
                return this.content;
            }
        }
        /**
         * @author:lxrm
         * @date:20161221
         * @description:spring-boot使用实例: hello world程序
         * @function:本程序作为Controller,接收请求,调用相关业务层组件来完成处理任务,并返回处理结果
         *             In Spring’s approach to building RESTful web services, 
         *             HTTP requests are handled by a controller.
         *              These components are easily identified by the @RestController annotation, 
         *             and the GreetingController below handles GET requests for /greeting 
         *             by returning a new instance of the Greeting class:*/
        package hello;
        
        import java.util.concurrent.atomic.AtomicLong;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestParam;
        import org.springframework.web.bind.annotation.RestController;
        
        @RestController
        public class GreetingController {
            private satic final String template="Hello,%s!";
            private final AtomicLong counter=new AtomicLong();
            
            /**About @RequestMapping annotation 
             *         1.The @RequestMapping annotation ensures that HTTP requests to 
             *           /greeting are mapped to the greeting() method.
             * 
             *         2.The above example does not specify GET vs. PUT, POST, 
             *             and so forth, because @RequestMapping maps all HTTP operations by default.
             *              Use @RequestMapping(method=GET) to narrow this mapping.
             * 
             **About @RequestParam annotation
             *      1.@RequestParam 将请求中的参数name绑定到greeting()方法的参数name上. 
             *          This query string parameter is explicitly marked as optional (required=true by default):
             *           if it is absent in the request, the defaultValue of "World" is used.*/
            @RequestMapping("/greeting")
            public Greeting greeting(@RequestParam(value="name", defaultValue="LXRM") String name) {
                /*
                 * @return:创建一个Greeting对象并作为返回值返回
                 *             */
                return new Greeting(counter.incrementAndGet(),
                                    String.format(template, name));
            }
        }
        
        
        /**
         * @author lxrm
         * @date 20161221
         * @description:spring boot的入门程序:hello world程序
         * @function:1)这个类使用@SpringBootApplication注解以及其子注解标签来使得your project中所添加的所有spring注解生效,
         *                 有了@SpringBootApplication注解以及其子注解标签(@Configuration、@EnableAutoConfiguration、@EnableWebMvc
         *                 @ComponentScan),
         *                 就可以不用使用*.xml配置文件,从而使得 there wasn’t a single line of XML? No web.xml file either. 
         *                 最终整个web application is 100% pure Java
         *              2)这个类中添加了public static void main(String[] args)函数,这就使得整个project可以被打包成可执行的jar包,
         *                 该jar包中包含所有necessary dependencies, classes, and resources,可以被直接运行
         * */
        package hello;
        
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.context.annotation.ComponentScan;
        import org.springframework.context.annotation.Configuration;
        import org.springframework.web.servlet.config.annotation.EnableWebMvc;
        /**
         * About @SpringBootApplication 注解
         *             @SpringBootApplication is a convenience annotation that adds all of the following:
         *                 @Configuration tags the class as a source of bean definitions for the application context.
         *                 @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings,
         *                                         other beans, and various property settings.
         *                 @EnableWebMvc Normally you would add this annotion for a Spring MVC app, 
         *                                 but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. 
         *                                 This flags the application as a web application and activates key behaviors 
         *                                 such as setting up a DispatcherServlet.
         *                 @ComponentScan(basePackage="包名") tells Spring to look for other components, configurations, 
         *                                 and services in the the hello package, allowing it to find the controllers.
                        
                        */
        @Configuration
        @EnableWebMvc
        @ComponentScan(basePackages = "hello")
        public class GreetingConfiguration {
            /**
             * The main() method uses Spring Boot’s SpringApplication.run() method 
             * to launch an application. */
            /*
            public static void main(String[] args) {
                SpringApplication.run(Application.class, args);
            }
            */
        }
        
        
        /**
         * @author lxrm
         * @date 20161221
         * @description spring boot框架下的第一个程序:helloWorld程序
         * @function 1.替代在web.xml中定义的任何spring相关的配置
         *              2.这个类的功能类似于web.xml配置文件的功能,传统web application 中是通过在web.xml中添加相应的配置
         *                如web.xml配置与springMVC相关的DispacthServlet来拦截客户端传来的HTTP请求,并交由spring的controller类处理相关请求,
         *                如配置spring注解相关的类,你的project中添加的与spring框架相关的注解才能被识别
         *              3.spring 4版本中舍弃了*.xml配置文件,直接使用java程序来进行这些配置
         *                 XxxConfiguration类使得spring注解生效
         *                 XxxInitializer.java(本程序)使得XxxConfiguration类生效,并且配置web.xml中所配置的其他东西*/
        package hello;
        
        import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
        
        public class GreetingInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
        
            @Override
            protected Class<?>[] getRootConfigClasses() {
                return new Class[] { GreetingConfiguration.class };
            }
         
            @Override
            protected Class<?>[] getServletConfigClasses() {
                return null;
            }
         
            @Override
            protected String[] getServletMappings() {
                return new String[] { "/" };
            }
        
        }