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

文章內容概述:html

  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更便捷。java

  咱們想要搭建的機器學習管理平臺是用於提供restful web services,綜合考慮spring社區各個projects的特色,決定捨棄原來的決定,由直接基於the spring framework搭建本身的web網站改成基於spring boot來搭建本身的網站,但願由此簡化整個開發過程。git

具體操做:程序員

  step1,瞭解spring boot的特色,參見官網documentationgithub

  step2, 參照  官網guids  以及 git上的sample工程 向項目中引入spring bootweb

        step2.1 修改pom.xml,包括spring

              註釋掉以前引入的spring framework的modules相關的配置,以及express

              添加spring boot相關的配置apache

            項目最終的pom.xml以下json

          • <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[] { "/" };
            }
        
        }
                     
相關文章
相關標籤/搜索