springboot自定义Starter(超简捷)

发布时间 2023-11-04 10:32:09作者: Amani_Bey

啥也不说,新建一个新的Maven项目引入Spring必要依赖
autoconfigure 这个依赖是 Spring Boot 框架的自动配置依赖,它包含了大量的自动配置类,用于根据应用程序的配置和环境,在应用程序启动时自动配置各种组件和属性。通过这个依赖,可以实现一些常见的配置,如数据库连接、缓存、消息队列等的自动配置。
processor 这个依赖是 Spring Boot 框架的配置处理依赖,它是一个编译时的依赖,用于在开发阶段帮助编辑器或 IDE 进行属性注入的代码补全和验证。通过这个依赖,可以使得使用 @Value 注解注入属性时,编辑器能够智能感知并提供代码补全。由于这个依赖只是在开发阶段使用,因此在打包时会被标记为 optional 属性,不会被打包进最终的可执行 JAR 或 WAR 中。

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




自定义一个服务:

public class CustomService {

    @Autowired
    private CustomProperties customProperties;

    public void customService() {
        System.out.println("customService");
        System.out.println(customProperties.toString());
    }
}




通过@Bean注入到ioc容器内

@Configuration
@Import({CustomProperties.class})
public class AutoConfig {
    @Bean
    public CustomService customService(){
        return new CustomService();
    }
}




然后在res目录下创建 yaml文件

project:
  version: 1.0.0

spring:
  application:
    name: custom-application

custom:
  application:
    name: ${spring.application.name}
    version: ${project.version}
    description: "custom starter 0.0.1.RELEASE"
    packageName: "com.example"
    packageVersion: "1.0.0"
    packageDescription: "custom starter 2.0.0.RELEASE"
    packageUrl: "https://custom starter/projects/spring-boot"
    packageLicense: "Apache License, Version 2.0"




好了开始通过自定义类读取yaml文件, AutoConfig配置类使用了 @Import({CustomProperties.class}) 是将 CustomProperties 类导入到应用程序上下文中,并将其视为一个 Bean 加载进来
例如,如果 CustomProperties 类中定义了名为 "app.name" 的属性,并且我们在应用程序中使用 @Value("#{customProperties.app.name}") 注解来注入该属性的值,那么 Spring Boot 会将 CustomProperties 类导入到应用程序上下文中,并将 "app.name" 属性的值注入到相应的字段或方法中。 总之,@Import({CustomProperties.class}) 注解是 Spring Boot 中用于加载 CustomProperties 类并将其引入应用程序上下文的便捷方式。


@Configuration
@ConfigurationProperties(prefix = "custom.application")
public class CustomProperties {
    private String name;
    private String version;
    private String applicationName;
    private String packageName;
    private String description;
    private String packageVersion;
    private String packageDescription;
    private String packageUrl;
    private String packageLicense;
}
......
setter
getter
toString




然后在resources 目录下创建META-INF目录,接着创建spring.factories 文件
META-INF 是一个标准的 Java 路径,用于指示一个 JAR 文件中的元信息文件夹。在 JAR 文件中,可以使用 META-INF 文件夹来存储一些关于该 JAR 文件的元数据信息,例如 MANIFEST.MF 文件、spring.factories 文件等。 spring.factories 是一个文本文件,用于存储 Spring 框架中的服务提供者和消费者的信息。它以 org.springframework.core.env.ConfigurableEnvironment 为键,以服务提供者的类名作为值,多个服务提供者的类名可以使用逗号分隔。在 Spring Boot 中,spring.factories 文件常用于自动配置应用程序,通过在其中添加键值对,可以标识应用程序中的自动配置类,从而实现自动配置。 例如,下面的 spring.factories 文件中的内容表示应用程序中使用了 com.example.MyConfiguration 类作为自动配置类:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=top.jilijili.starter.config.AutoConfig

通过在 META-INF/spring.factories 文件中添加类似的键值对,可以告诉 Spring Boot 在启动应用程序时应该加载哪些自动配置类。




接着继续创建一个新的maven项目,引入自定义starter依赖
启动类

@SpringBootApplication
public class App {


    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
        // 自定义依赖下的服务
        CustomService bean = context.getBean(CustomService.class);
        bean.customService(); // 输出:customService
CustomProperties{name='custom-application', version='2.1.12', applicationName='custom', packageName='com.example', description='custom starter 0.0.1.RELEASE', packageVersion='1.0.0', packageDescription='custom starter 2.0.0.RELEASE', packageUrl='https://custom starter/projects/spring-boot', packageLicense='Apache License, Version 2.0'}

    }
}
<br/><br/>

可以在yaml文件重新配置自定义starter下的yaml文件属性:



在自定义依赖下面,编译解释过后的target目录下能看到:spring-configuration-metadata.json 文件



好,到此结束