天天看点

SpringBoot系列---【如何自定义Starter】

一、前置知识:

maven项目打包参考连接

二、自定义starter

1.命名规范

  自定义的Starter官方建议:叫xxx-spring-boot-starter。

2.新建maven项目,结构如下

SpringBoot系列---【如何自定义Starter】

3.添加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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.fast</groupId>
    <artifactId>fast-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>fast-spring-boot-starter</name>
    <description>fast-spring-boot-starter</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.6.7</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.16</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.7</version>
        </dependency>
    </dependencies>
</project>      

4.编写自己的核心代码Myservice.java

package com.fast;

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

@EnableConfigurationProperties(MyService.class)
//注意:prefix的值不能用驼峰命名,springboot2.x不识别,例:myService新项目引入导致无法启动,正例:myservice或my-service
@ConfigurationProperties(prefix = "myservice")
public class MyService {
    private int id;
    private String name;

    //构造方法,set,get,toString省略
}      

5.编写自动配置类MyServiceAutoConfiguration.java

package com.fast;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyServiceAutoConfiguration {
    static {
        System.out.println("加载MyServiceAutoConfiguration");
    }

    @Bean
   //注意紫色部分叫什么,使用@Autowaired注入的时候就叫什么
    public MyService myService() {
        return new MyService();
    }
}      

6.在Resource下新建META-INF目录,再在新目录下新建spring.factories

紫色部分为固定写法,后面跟自己的自动配置类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.fast.MyServiceAutoConfiguration      

7.install或上传到私服,在新项目的pom中引用即可。

测试:

1.在新项目的application.yml中添加myservice

myservice:
  id: 11
  name: 李四      

2.在测试类中编写测试

注意:包结构和src/java下的包结构要一致,否则会报错。

SpringBoot系列---【如何自定义Starter】
package com.fast;

import org.junit.Test;
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.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SysJobTest {
    @Autowired
    private MyService myService;

    @Test
    public void startTest() throws Exception {
        System.out.println("myService = " + myService);
    }
}      

三、热插拔

  如果不使用热插拔,坐标引入即意味着功能开启,如果使用了热插拔,则使用条件触发,例如:在启动类上添加指定注解才允许开启。

1.把上面的工程接着改造,结构如下

SpringBoot系列---【如何自定义Starter】

 2.新增标记类ConfigMarker.java

package com.fast;

/**
 * 标记类
 */
public class ConfigMarker {
}      

3.新增注解开关EnableRegisterServer 

package com.fast;

import org.springframework.context.annotation.Import;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
//@Import这个注解是热插拔的核心,这个注解会将ConfigMapper生成实例,并注入到容器中
@Import({ConfigMarker.class})
public @interface EnableRegisterServer {
}      

4.改造自动配置类

注意紫色部分,改造好之后,重新install或上传到私服

package com.fast;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
//检测到容器中有ConfigMarker实例存在,就会自动加载下面的代码,反之,不加载
@ConditionalOnBean(ConfigMarker.class)
public class MyServiceAutoConfiguration {
    static {
        System.out.println("加载MyServiceAutoConfiguration");
    }

    @Bean
    public MyService myService() {
        return new MyService();
    }
}      

5.在新项目中的启动类上添加开关注解

package com.fast;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.DependsOn;

@SpringBootApplication
//注掉这个注解,则项目启动时不会加载fast-spring-boot-starter的myService,所以容器中没有这个对象;
//放开这个注解,则项目启动时会在ioc容器中注入myService实例,做到了自动加载自定义starter
@EnableRegisterServer
@MapperScan("com.fast.dao")
public class FastBootApplication {

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