首页 > 技术文章 > 1.Spring Boot入门及其jar包依赖模型分析

hzhuxin 2017-10-23 17:52 原文

Spring Boot介绍

Spring Boot是由Pivotal团队提供的新框架,其设计目的是简化Spring应用的搭建以及开发过程。其目标是:

  • 为所有Spring开发提供一个从根本上更快,且方便上手的体验
  • 开箱即用,摆脱原有的默认配置方式
  • 提供大型项目常用的非功能性特征,例如:内嵌服务器,安全,指标度量,健康监测,外部化配置
  • 无需xml配置和绝无代码生成

Spring Boot系统要求

Spring Boot 2.1.2.RELEASE 版本 要求Java7或者更高版本(推荐用jdk8)、Spring Framework 4.3.12.RELEASE或更高版本。构建项目可以通过Maven(3.2+)或Gradle3(3.4或更高版本)。

 

Spring Boot快速入门例子

pom文件配置如下(关注粉红色部分)


<?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>org.huxin.demo</groupId>
    <artifactId>spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- 请注意,它的 type 是 pom,scope 是 import,这种类型的 dependency 只能在 dependencyManagement 标签中声明。因为它是要在当前项目的依赖声明列表中
引用某个依赖中的<dependencymanagement/>下的依赖声明列表
--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies>       <dependency>         <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

 

启动类package org.huxin.demo.springboot

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Configuration
@ComponentScan //这个注解告诉spring,以当前类所在的package为basePackage,来扫描Bean @EnableAutoConfiguration //这个注解的另一个作用会通过分析我们当前项目的类路径下是否含有一些特定的类,以此来为我们动态创建相应的Bean,这正是其AutoConfiguration(自动配置)的作用
public class Examples { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Examples.class, args); } }

pom文件也可以配成这样( 即继承Spring Boot的父项目spring-boot-starter-parent )

<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>org.huxin.demo</groupId>
    <artifactId>spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
  </parent>

    <dependencies>
<!--spring-boot-starter依赖可省略-->     <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter</artifactId>     </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>

 

 就这样只一个pom配置文件加上一个Controller和一个main方法即完成了一个简单的应用的开发。启动main方法,通过http://localhost:8080/即能访问。

为了更深入的理解,我们用类图的方式简单分析一下pom中的依赖关系:

从上图可以看到,为什么会有两种配置方式呢,原来springboot这个框架对外能提供的starter(或者称XX功能的jar集合)都放在spring-boot-dependencies这个父项目中,所以关键在于如何在pom中引入它。从图中可以看出通过spring-boot-starter-web项目的继承机制是能引用到spring-boot-dependencies这个父项目的,但是缺少一个版本号的提供,所以当我们不指定spring-boot-starter-parent 作为父项目时(因为此时指定spring-boot-starter-parent时会带版本号),即不想借助继承机制获取这个父项目,就需要通过如下方式引入,这里其实涉及到了maven的一个知识点,即maven的BOM功能。 

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.1.2.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

但如果明白整个原理我觉得也可以用如下方式,经测试也是可行的

<?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.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring.boot.version>2.1.2.RELEASE</spring.boot.version>
    </properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
             <version>${spring.boot.version}</version>
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

所以其实是有三种方式来配置springboot项目的pom文件的。

下面我们还需要考虑一个问题,就是在生产环境下该如何部署这个小项目,我们需要在打好的jar中包含依赖的jar,这样才能方便的在生产环境下启动项目。Spring Boot已经帮我们打理好了这一切,如果项目是继承自 spring-boot-starter-parent ,在pom文件中插入下段配置就可以

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

但如果项目不是继承于 spring-boot-starter-parent ,则要配置如下

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

这样再用mvn install进行打包,就会发现jar的大小比以前大了不少。打好包以后,进入命令行,执行  java -jar f:/temp/spring-boot-0.0.1.jar 就可以把我们的项目启动起来了。

 

推荐阅读