软件架构-Spring boot快速开始及核心功能介绍(中)

1,074 阅读3分钟

上次通过Spring boot认知,核心功能。springBoot的搭建【官方向导搭建boot应用】和 【maven的方式搭建boot】。

统一父POM管理(一)

  • ① 建立boot-parent工程

首先我们建立一个 boot-parent的maven工程

删除src目录

然后修改pom.xml。packaging改为为pom格式。

<packaging>pom</packaging>

加入dependencyManagement, 同时去掉version, 直接使用父pom中的版本即可。

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

加入 build

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

加入properties添加

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

父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>com.idig8.springboot</groupId>
    <artifactId>springboot-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>vip-springboot-parent</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-parent</artifactId>
                <version>1.5.10.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

  • ② 创建module-base项目

加入java文件

package com.idig8.springboot;

/**
 * @program: springboot-second
 * @description: ${description}
 * @author: LiMing
 * @create: 2019-06-04 22:42
 **/
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;


@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }

}

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">
    <parent>
        <artifactId>springboot-parent</artifactId>
        <groupId>com.idig8.springboot</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>com.idig8.springboot-base</artifactId>
    <name>springboot-base</name>
    <url>http://maven.apache.org</url>
    <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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

然后访问:http://localhost:8080/ spring boot一个很重要的特点:解决了所有依赖的版本问题。

spring boot 测试(二)

添加测试支持依赖:spring-boot-starter-test

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

注意:加入这个依赖之后,junit包就可以不用了,因为test的starter中包含了junit。备注:怎么找到所有的starter。进入官网查询Reference:start.spring.io/

这里面ctrl +f 搜索:starter,就可以看到spring boot中的所有starter 编写测试类

package com.idig8.springboot;

import junit.framework.TestCase;

import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;


@Ignore
@SpringBootTest(classes=Example.class)
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ExampleTest {
    @Autowired
    private Example controller;

    //	@Test
    public void testHome() {
        TestCase.assertEquals("Hello World!", controller.home());
        System.out.println(controller.home());
    }
}

spring boot 启动注解分析(三)

  • ① @EnableAutoConfiguration

开启自动配置功能 @ComponentScan(basePackages={"com.example.boot"}) 包扫描。

  • ② @SpringBootApplication配置详解:

一个组合注解,他内部主要包含三个子注解:@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan。

  • ③ @SpringBootConfiguration

继承@Configuration,说明这是一个配置类,什么是配置类呢?就相当于我们以前写的xml配置,例如我们我们的bean标签,用来实例化一个bean。那么在这个配置类中就是实现以前我们xml配置的功能。

  • ④ @EnableAutoConfiguration

开启自动配置功能,他会扫描带有@Configuration的类,然后初始化这些配置类中的信息并且加入到应用上下文中去,同时完成一些基本的初始化工作。场景:redis的默认配置。数据库连接池的大小配置。

  • ⑤ @ComponentScan

组件包扫描,也就是我现在需要扫描哪些包下面的注解,可自动发现和装配一些bean。默认扫描当前启动类所在包下面的类和下面的所有子包。

spring boot 热加载/部署(四)

  • ① springloaded
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>springloaded</artifactId>
</dependency>
  • ② spring-boot-devtools
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

启动程序,访问浏览器出现第一个结果,然后修改控制器输出内容,再次刷新看到新的结果,同时在控制台可以看待这样一句话: o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729

项目打包部署(五)

修改boot-parent中pom.xml文件,增加如下内容(当然也可以把下面的内容复制到子模块中也是可以的)

 <build>
        <plugins>
            <plugin><!-- 项目的打包发布 -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.idig8.springboot.Example</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

执行maven install

在target目录下面,可以看到打包的jar文件

image.png

执行java -jar xx.jar

【注意:执行jar的jdk版本需要与jar打包编译的版本一致。如果配置了环境变量,直接使用java命令打包即可】

PS:SpringBoot的入门基本到这里,这就是微架构,一个程序打包之后轻轻松松在如任何地方一执行就完成了。