自定义SpringBoot的starter

1.自定义starter名为my-starter-spring-boot-starter

1.1 idea中创建一个maven模块

groupId为com.example
artifactId为my-starter-spring-boot-starter
起名规范:
1.官方starter是spring-boot-starter-xxxx
2.自定义starter是xxx-spring-boot-starter
依赖如下

<?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>com.example</groupId>
    <artifactId>my-starter-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>my-starter</name>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

	<!-- 引入必须的autoconfigure的依赖-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.6.2</version>
        </dependency>
    </dependencies>

</project>

1.2 创建properties类,用于接收application.properties中指定的属性字段及值

@ConfigurationProperties(prefix = "com.example")
public class MyStarterProperties {

    private String name;

    public String getName() {
        return name;
    }

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

1.3 创建配置类,用于将properties类中的数据对应到配置上

public class MyStarterConfig {

    private String name;

    private MyStarterProperties myStarterProperties;

    public MyStarterConfig(MyStarterProperties myStarterProperties) {
        this.myStarterProperties = myStarterProperties;
    }

    public String getName() {
        return myStarterProperties.getName();
    }

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

1.4 开启自动配置

@Configuration
@EnableConfigurationProperties(MyStarterProperties.class) // 开启配置文件生效
public class MyStarterAutoConfiguration {

    @Autowired
    MyStarterProperties myStarterProperties;

    @Bean
    @ConditionalOnMissingBean(MyStarterConfig.class)
    public MyStarterConfig myStarterConfig(){
        return new MyStarterConfig(myStarterProperties);
    }
}

1.5 resources/META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.springboot.configuration.MyStarterAutoConfiguration

1.6 resources/META-INF/spring-configuration-metadata.json

{
  "group": [
    {
      "name": "com.example",
      "type": "com.example.springboot.MyStarterProperties",
      "sourceType": "com.example.springboot.MyStarterProperties"
    }
  ],
  "properties": [
    {
      "name": "com.example.name", // 配置变量名
      "type": "java.lang.String",
      "description": "my start name",
      "sourceType": "com.example.springboot.MyStarterProperties",
      "defaultValue": "MyStarterProperties name"
    }
  ]
}

1.5 代码树结构

image

1.6 构建编译打包到本地

clean->install

2.使用自定义starter

2.1 另一个项目中引用

<dependency>
            <groupId>com.example</groupId>
            <artifactId>my-starter-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

2.2 application.properties配置文件中添加自定义的属性

com.example.name=hanboyuan

2.3 单测使用starter中的自定义配置

@RunWith(SpringRunner.class)
@Slf4j
@SpringBootTest(classes = StarterTest.class)
@SpringBootApplication(scanBasePackages = {"delta.*"},
        exclude = {DataSourceAutoConfiguration.class, MybatisPlusAutoConfiguration.class})
public class StarterTest {

    @Autowired
    MyStarterConfig myStarterConfig;

    @Test
    public void testName(){
        String name = myStarterConfig.getName();
        log.info("name:{}",name);
    }
}
posted @ 2023-06-12 16:28  SpecialSpeculator  阅读(96)  评论(0编辑  收藏  举报