文章内容概述:

  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[] { "/" };
        } }
         

                     

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

  1. Eclipse导入Spring Boot项目后pom.xml出现红叉的解决办法

    胸怀难的问题是:程序能正常运行,但是pom.xml下面有一个红叉. 解决办法: 右键项目 --> Update project...

  2. STS创建spring boot项目,pom.xml文件第一行报错

    亲测能用url地址:https://blog.csdn.net/jrx1995/article/details/100008552

  3. ARC工程中添加非ARC文件

    转载自:http://blog.csdn.net/zhenweicao/article/details/16988543 分类: IOS2013-11-27 17:02 626人阅读 评论(0) 收藏 ...

  4. 工程中添加工程依赖 Xcode iOS

    有时我们需要在一个主工程中添加其他的子工程,用来对子工程进行编写修改或者是利用子工程中的库文件等等操作,这时候我们需要用到工程的嵌套.   步骤:(看图说话)   1.新建主工程,名为TestTTTT ...

  5. 创建maven项目时pom.xml报错的解决方法

    创建maven项目时pom.xml时: 出现如下报错信息: Failure to transfer commons-lang:commons-lang:jar:2.1 from https://rep ...

  6. WPF - 为什么不能往Library的工程中添加WPF window

    项目中添加一个Library 工程,但是却无法加入WPF window, WPF customize control. 调查了一下,发现这一切都由于Library工程中没有:ProjectTypeGu ...

  7. ios如何在当前工程中添加编辑新建的FramesWork

    本文转载至 http://www.apkbus.com/android-131519-1-1.html,感谢原文作者的分享.     naniboy 该用户从未签到   可能很多大牛都见过FaceBo ...

  8. 关于在工程中添加新文件时的LNK2019错误的一个解决办法

    我这几天一直在研究Qt的串口程序,在读懂了官方给出的实例程序后我决定把其多线程的串口监视程序加入到我自己的工程中,便直接把问价复制到自己的工程下面,在Qt中加入到自己的工程中,但是总是出现LNK201 ...

  9. IOS学习:在工程中添加百度地图SDK

    1.将下载下来的sdk中的inc文件夹.mapapi.bundle.libbaidumapapi.a添加到工程中,其中libbaiduapi.a有两个,一个对应模拟器一个对应真机,导入方法如下: 第一 ...

随机推荐

  1. hdu 4348 To the moon

    题意:n个数 m次操作 操作分别为 C l r d: 把区间[l, r] 加 d Q l r : 查询区间[l, r]的和 H l r t: 查询时间t的时候[l, r]的和 B t: 回到时间t 思 ...

  2. 20151214下拉列表:DropDownList

    注意: .如果用事件的话就要把控件的AutoPostBack设置成true .防止网页刷新用一个判断 if (!IsPostBack)//判断是第一个开始还是取的返回值 { } 下拉列表:DropDo ...

  3. JSON.parse()的正确用法

    昨天晚上在项目中使用JSON.parse()来将字符串格式的数据转换成json,结果悲剧了,总感觉方法没有用错,可是就是报错!想了好久,最后发现原来是json字符串格式不标准! 如:var a = “ ...

  4. Install Qt creator

    download qt for linux yum install dialog move download qt file(qt-opensource-linux-x64-5.6.0.run) fr ...

  5. js反射机制

    本文转载自:http://blog.csdn.net/liuzizi888/article/details/6632434 什么是反射机制反射机制指的是程序在运行时能够获取自身的信息.例如一个对象能够 ...

  6. <<google软件测试之道>>读书笔记

    以前一直从开发的角度来看待测试,看完这本书以后感觉错了,难怪之前公司的测试一直搭建不起来 1.开发人员,开发测试人员,测试人员 * 开发人员负责开发 * 开发测试人员近距离接触代码,负责编写测试用例, ...

  7. ant 自定义taskdef的工作目录

    上次同事在用ant执行多层目录的测试用例的时候遇到了一些问题,也就是自定义的taskdef的工作目录总是在开始执行ant的那个目录,而有一些地方用到了当前目录,因此很多测试用命的代码出现了“找不到自定 ...

  8. Python正则表达式指南

    1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十 ...

  9. mac与php环境

    一.目录 apache目录:/etc/apache2/ mysql目录:/usr/local/mysql/ 站点目录:/Library/WebServer/Documents/ 二.mac系统给文件夹 ...

  10. 浅谈可扩展性框架:MEF

    之前在使用Prism框架时接触到了可扩展性框架MEF(Managed Extensibility Framework),体验到MEF带来的极大的便利性与可扩展性. 此篇将编写一个可组合的应用程序,帮助 ...