右耳菌-邓小白的Java架构师的修炼之路spring boot

SpringBoot 的 Starter 快速集成机制

2022-07-19  本文已影响0人  右耳菌

一、Starter介绍


二、Web开发相关

引入spring-boot-starter-web实现快速引入和启动,无需再进行繁杂的xml配置。
默认基于Tomcat容器运行,可通过修改pom.xml指定运行的容器。


修改pom.xml指定运行的容器

疑问:
为什么我们明明没有在Application中指定我们引入的jar包的内容,但是却能正常扫描到引入的包的内容?

我们在SpringApplication.run这个方法中打上断点,然后进行查看:

这里我们可以看到这里是加载了一个叫做 META-INF/spring.factories的文件,这里打开autoconfigure的spring.factories

这里其实是很多的配置类的信息,即具体的文件路径等等,其实继续查看代码,发现他们会自动加载这些配置类。由此可想而知,正是这个原因,所以才能正常的将引入的jar包的内容扫描加载进来。

另外我们还可以看到在META-INF文件夹中,还有一个叫做 spring-configuration-metadata.json 的文件,这个文件大概的内容如下所示:

这里其实就是在编写yml和properties中,会自动弹出一些提示信息,方便用户快速编写相关的内容。


三、自研Starter的步骤

  1. 建工程
  2. 引入spring-boot-starter、spring-boot-autoconfigure、第三方jar
  3. 如需要生成配置元信息,加入spring-boot-configuration-processor依赖
  4. 编写自动配置类
  5. 配置发现配置文件:META-INF/spring.factories
  6. 打包发布
例子:

众所周知,男性程序员还是需要女朋友的,所以我们这次用女孩子作为一个project,手动狗头~~~
首先创建三个项目,如图


image.png

然后我们一步一步按照上边的步骤来,但是我们首先要实现girl 这个项目的内容(即第三方jar)

  1. 这里比较简单,在girl项目中,创建类GirlDemo即可,但是也要记得修改pom文件。
package cn.lazyfennec.girl;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/7/19 22:15
 */
public class GirlDemo {

    private String name;

    private Integer height;

    private String face;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public String getFace() {
        return face;
    }

    public void setFace(String face) {
        this.face = face;
    }

    public void doSomething() {
        System.out.println("do something in here!");
    }

    @Override
    public String toString() {
        return "GirlDemo[" +
                "name='" + name + '\'' +
                ", height=" + height +
                ", face='" + face + '\'' +
                ']';
    }
}
<?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>cn.lazyfennec</groupId>
    <artifactId>girl</artifactId>
    <version>1.0.0</version>
    <description>The Demo of girl demo</description>
    <packaging>jar</packaging>

    <name>girl</name>
    <url>http://maven.apache.org</url>

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

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

  1. 修改girl-spring-boot-starter的pom.xml
<?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>cn.lazyfennec</groupId>
    <artifactId>girl-spring-boot-starter</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

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

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

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- 第三方jar依赖 -->
        <dependency>
            <groupId>cn.lazyfennec</groupId>
            <artifactId>girl</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!-- 其他的相关依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.0.8.BUILD-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.0.8.BUILD-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.0.3.RELEASE</version>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>META-INF</directory>
                <targetPath>META-INF/</targetPath>
            </resource>
        </resources>
    </build>
</project>

  1. 创建GirlProperties 类,这里主要是为了实现自动读取并且注入配置文件信息的功能
package cn.lazyfennec.girl.spring.boot.autoconfigure;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/7/19 22:24
 */
// 这个地方需要EnableConfigurationProperties 配置指向这个类,否则会爆红提示错误,其实也就是下一步
@ConfigurationProperties(prefix = "cn.lazyfennec.girl") 
public class GirlProperties {
    private String name;
    private Integer height;
    private String face;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public String getFace() {
        return face;
    }

    public void setFace(String face) {
        this.face = face;
    }
}

  1. 创建GirlAutoconfigure类,这里其实就是读取配置文件,并且根据配置文件生成bean对象
package cn.lazyfennec.girl.spring.boot.autoconfigure;

import cn.lazyfennec.girl.GirlDemo;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/7/19 22:40
 */
@Configuration
@EnableConfigurationProperties({GirlProperties.class})
public class GirlAutoconfigure {

    @Bean
    public GirlDemo getGirl(GirlProperties properties) {
        GirlDemo girl = new GirlDemo();
        girl.setName(properties.getName());
        girl.setHeight(properties.getHeight());
        girl.setFace(properties.getFace());
        return girl;
    }
}

  1. 创建src同级的目录下(好像放到resources目录下也行)META-INF/spring.factories 文件,然后修改一些内容
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.lazyfennec.girl.spring.boot.autoconfigure.GirlAutoconfigure

  1. 可以配置spring-configuration-metadata.json 的内容以方便在配置文件中进行配置,但是这里因为太懒,所以就不进行编写了

  1. 修改springboot-demo 的启动类
package cn.lazyfennec.demo;

import cn.lazyfennec.girl.GirlDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SpringBootDemoApplication {

    @Autowired
    private GirlDemo g;

    @RequestMapping("/girl")
    public String getGirl() {
        return g.toString();
    }

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

}

  1. 修改application.yml(properties文件的修改尽管有些格式上的不同,但是很类似,这里不进行描述了)
server:
  port: 8090

cn:
  lazyfennec:
    girl:
      name: 小白
      height: 161
      face: 唉哟,还不错哦

  1. 打开浏览器进行测试



如果觉得有收获就点个赞吧,更多知识,请点击关注查看我的主页信息哦~

上一篇 下一篇

猜你喜欢

热点阅读