动手写一个Starter

2020-08-18  本文已影响0人  topshi

Starter启动器

顾名思义,启动器,一站式启动然后就可以使用该starter提供的功能了。它包含了某一功能的所有依赖,比如springboot的web启动器,我们只需要在pom文件中引入该启动器依赖就可以使用web相关功能。可以通俗的说,starter是一个功能模块。

写一个Starter

接下来,我们写一个Starter。该Starter自动配置一个Bean提供一个打印功能。

项目文件有三个:

创建Maven项目,该项目是没有启动类和test文件夹的。
引入相关依赖,主要是spring-boot-autoconfigurespring-boot-configuration-processor

  <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.17.BUILD-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xiucong</groupId>
    <artifactId>helloworld-springboot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>helloworld-springboot-starter</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
    </dependencies>
</project>

  1. 首先是我们的HelloClient类,该类的实例将会被我们的自动配置类加入到容器中,其提供一个打印功能。
package com.xiucong.helloworld;

/**
 * @ClassName HelloClient
 * @Description TODO
 * @Author xiuc_shi
 * @Date 2020/8/17 下午 10:26
 * @Version 1.0
 **/
public class HelloClient {
    private String message;

    public HelloClient(String message){
        this.message = message;
    }

    public void print(){
        System.out.println(message);
    }
}

  1. HelloProperties类,用@ConfigurationProperties(prefix = "hello")指定配置项的前缀。该类将配置文件中的属性加载作为自己属性的值。
package com.xiucong.helloworld;

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

/**
 * @ClassName HelloProperties
 * @Description TODO
 * @Author xiuc_shi
 * @Date 2020/8/17 下午 10:28
 * @Version 1.0
 **/

@ConfigurationProperties(prefix = "hello")
public class HelloProperties {

    //默认是hello world.,配置文件如果设置了hello.message的值,则覆盖hello world.
    private String message = "hello world.";

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

  1. HelloWorldAutoConfiguration自动配置类。在该类中,我们注意到一个注解@EnableConfigurationProperties(HelloProperties.class)使能我们的配置类,此时配置类实例被注册到了容器中。然后,我们可以拿到这个bean从而获取其属性值。
package com.xiucong.helloworld;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @ClassName HelloWorldAutoConfiguration
 * @Description TODO
 * @Author xiuc_shi
 * @Date 2020/8/17 下午 10:33
 * @Version 1.0
 **/

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloWorldAutoConfiguration {

    @Resource
    private HelloProperties properties;

    @Bean
    @ConditionalOnClass(HelloProperties.class)
    public HelloClient helloClient(){
        return new HelloClient(properties.getMessage());
    }
}

  1. 在Resources文件夹下创建一个META-INF目录,在该目录下创建文件spring.factories,指明自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration = \
  com.xiucong.helloworld.HelloWorldAutoConfiguration
  1. 完成,使用maven安装install到本地仓库

测试我们的Starter

创建Springboot应用,然后引入我们的Starter依赖,该依赖可以见上面pom文件的<parent>标签下方

    <groupId>com.xiucong</groupId>
    <artifactId>helloworld-springboot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
上一篇 下一篇

猜你喜欢

热点阅读