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

         

         

         

                     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM