springboot2.x——依赖的统一管理

springboot2.x——依赖的统一管理  1、创建统一管理依赖库的项目:dependencies  
图片描述

  这个项目仅仅对依赖库的管理,pom.xml文件简单引入springboot的父工程:
  

 <?xml version="1.0" encoding="UTF-8"?>
 <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/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 
     <!--工程信息-->
     <groupId>com.baiye</groupId>
     <artifactId>spring-boot-dependencies</artifactId>
     <version>1.0.0-SNAPSHOT</version>
     <packaging>pom</packaging>
 
     <name>spring-boot-dependencies</name>
     <description></description>
     <!--spring-boot 依赖的父工程-->
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.0.3.RELEASE</version>
         <relativePath/> <!-- lookup parent from repository -->
     </parent>
     <!--设置第三方依赖库版本号-->
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <java.version>1.8</java.version>
     </properties>
     <!--设置第三方依赖库-->
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-thymeleaf</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <scope>runtime</scope>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
     </dependencies>
 
    <!-- <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
     </build>-->
 
 
 </project>

打包方式是pom形式。2、创建hello项目测试,步骤与dependencies类似。不同的是:pom.xml不需要引入springboot的父工程,而是引入dependencies。

 <?xml version="1.0" encoding="UTF-8"?>
 <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/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 
     <groupId>com.baiye</groupId>
     <artifactId>spring-boot-hello</artifactId>
     <version>1.0.0-SNAPSHOT</version>
     <packaging>jar</packaging>
 
     <name>spring-boot-hello</name>
     <description></description>
 
     <!--自定义继承父工程dependencies-->
     <parent>
         <groupId>com.baiye</groupId>
         <artifactId>spring-boot-dependencies</artifactId>
         <version>1.0.0-SNAPSHOT</version>
         <!--相对路径:引入父工程的pom.xml-->
         <relativePath>../spring-boot-dependencies/pom.xml</relativePath>
     </parent>
     <!--设置本项目特有的第三方依赖库-->
     <dependencies>
         
     </dependencies>
 
     <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
     </build>
 
 
 </project>

至此项目构建完毕,请仔细观看项目的目录结构:

图片描述

接下来简单写一个hello例子,测试是否成功自定义的父类依赖,目录结构如下:

图片描述

HelloController.java

 package com.baiye.springboothello.controller;
 
 
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController
 public class HelloController {
     @RequestMapping(value = {"/",""},method = RequestMethod.GET)
     public String hello(){
         return "Hello Spring Boot";
     }
 }

启动main函数,运行如下:

图片描述
  与传统web项目对比:- 没有配置 web.xml

  • 没有配置 application.xml,Spring Boot 帮你配置了
  • 没有配置 application-mvc.xml,Spring Boot 帮你配置了
  • 没有配置 Tomcat,Spring Boot 内嵌了 Tomcat 容器