springboot如何自定义starter

在这里插入图片描述

1:定义starter

1.1: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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
  </parent>

  <groupId>dongshi.daddy</groupId>
  <artifactId>firstspringbootstart</artifactId>
  <version>1.0-SNAPSHOT</version>
  <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-starter</artifactId>
    </dependency>
  </dependencies>
</project>

1.2:配置类

不管是官方提供的starter还是自定义的starter,使用的时候都需要在配置文件中进行相关的配置,那么这些配置信息在starter中最终会映射到一个类中,因此我们需要首先定义这个类,需要使用到@ConfigurationProperties注解,该注解源码如下:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {
    @AliasFor("prefix")
    String value() default "";
    @AliasFor("value")
    String prefix() default "";
    boolean ignoreInvalidFields() default false;
    boolean ignoreUnknownFields() default true;
}

其中最重要的一个属性是preffix,该值对应的是我们在配置文件中设置的配置信息的前缀,即需要通过此前缀来进行相关信息的配置,然后类中定义需要配置的属性,则前缀+属性名就是我们需要在使用starter项目配置文件中配置的信息了,本例定义如下:

/**
 * 该类用于封装配置信息
 */
@ConfigurationProperties(prefix = "first")
public class MyFirstConfigurationProperties {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

1.3:服务类

服务类,即最终被调用执行具体功能的类,该类和定义普通类没有任何差别,如下:

public class FirstStarterService {
    private String name;
    private int age;

    public FirstStarterService() {}

    public FirstStarterService(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void introduceYourself() {
        String introduction = "我的名字是:%s,我的年龄是:%s";
        System.out.println(String.format(introduction, this.name, this.age));
    }
}

1.4:注册服务

通过bean configuration的方式来注册服务,因为需要使用到用户的配置信息所以需要通过@EnableConfigurationProperties注解来启用配置,并将配置类注入,这里是MyFirstConfigurationProperties,最后最终要的就是通过@Bean注解来注册服务类到容器中,这里就是FirstStarterService,最终代码如下:

@Configuration
@EnableConfigurationProperties(MyFirstConfigurationProperties.class)
public class MyFirstBeanConfig {

    @Autowired
    private MyFirstConfigurationProperties configurationProperties;

    // 注册bean
    @Bean(value = "firstStarterService")
    public FirstStarterService registerFirstStarterService() {
        return new FirstStarterService(configurationProperties.getName(), configurationProperties.getAge());
    }
}

1.5:暴露服务

在资源目录下创建META-INF/spring.factories文件,并将创建的bean configuration类的全限定名定义在里面,这里是dongshi.daddy.beanconfig.MyFirstBeanConfig,注意每个人不一样,记得更换为自己的。
在这里插入图片描述

org.springframework.boot.autoconfigure.EnableAutoConfiguration=dongshi.daddy.beanconfig.MyFirstBeanConfig

1.6:打jar包

mvn clean install。jar包中配置相关信息在META-INF下,主要有spring-configuration-metadata.json,该文件内容如下,为该starter可以配置的参数:

{
  "groups": [
    {
      "name": "first",
      "type": "dongshi.daddy.myconfigurationproperties.MyFirstConfigurationProperties",
      "sourceType": "dongshi.daddy.myconfigurationproperties.MyFirstConfigurationProperties"
    }
  ],
  "properties": [
    {
      "name": "first.age",
      "type": "java.lang.Integer",
      "sourceType": "dongshi.daddy.myconfigurationproperties.MyFirstConfigurationProperties",
      "defaultValue": 0
    },
    {
      "name": "first.name",
      "type": "java.lang.String",
      "sourceType": "dongshi.daddy.myconfigurationproperties.MyFirstConfigurationProperties"
    }
  ],
  "hints": []
}

因此这里可以配置的参数有first.age,first.name。还有文件spring.factories就是我们配置的暴露服务的文件。
在这里插入图片描述

2:使用starter

2.1:依赖starter的jar

    <dependency>
      <groupId>dongshi.daddy</groupId>
      <artifactId>firstspringbootstart</artifactId>
      <version>1.0-SNAPSHOT</version>
      <scope>system</scope>
      <systemPath>E:/program_file/maven/maven_rep/dongshi/daddy/firstspringbootstart/1.0-SNAPSHOT/firstspringbootstart-1.0-SNAPSHOT.jar</systemPath>
    </dependency>

2.2:添加配置

创建application.properties,并添加配置,如下:

server.port=8082
first.age=30
first.name=dongshidaddy

2.3:定义controller

@RestController
public class FirstSpringBootStarterTestController {

    //{"firstStarterService"})
    @Resource(name = "firstStarterService")
    public FirstStarterService firstStarterService;

    @RequestMapping(value = "/firstIntroduce")
    public void firstIntroduce(HttpServletResponse response) throws IOException {
        firstStarterService.introduceYourself();
        response.getWriter().write("test user custom starter");
    }
}

2.4:启动测试

在这里插入图片描述

3:源码

starter测试starter

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值