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

一、前置知识:

maven项目打包参考连接:https://www.cnblogs.com/hujunwei/p/16381945.html

二、自定义starter

1.命名规范

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

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

 

 

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下的包结构要一致,否则会报错。

复制代码
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.把上面的工程接着改造,结构如下

 

 

 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); } }
复制代码

至此,我们就完成了一个简单的自定义starter,并且支持热插拔。

 

posted on   少年攻城狮  阅读(958)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2021-06-16 常见的问题系列---【java: You aren’t using a compiler supported by lombok, so lombok will not work and has been disabled.】
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示