关于java:springBoot-自定义-starter-插件

8次阅读

共计 3221 个字符,预计需要花费 9 分钟才能阅读完成。

  • 例如 xxx-boot-start. 相似很多这种配置, 本人也能够实现本人的插件。多模块工程中插件模式很多,针对多模块工程写了几个插件。
maven 依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
  • 写 spring.factories 形式
@Configuration
@ConfigurationProperties(prefix = "cache")
@Data
public class CacheProperty {


    private Boolean enable;
    private long duration;
    private int initialCapacity;
    private long maximumSize;
    private String type;


}
@Configuration
// 扫描 ioc 的 bean 能够扫描第三方包
/**
 * 或者 应用
 * ComponentScan 包扫描 扫描包下的 bean 也行
 * EnableConfigurationProperties 注入配置类也能够
 */
@EnableConfigurationProperties(CacheProperty.class)
//@ComponentScan("org.redorblack.*")
public class CacheAutoConfiguration {




    @Bean
    public Cache<String, Object> creatCaffeineCache(CacheProperty cacheProperty) {return Caffeine.newBuilder()
                // 设置最初一次写入或拜访后通过固定工夫过期
                .expireAfterWrite(cacheProperty.getDuration(), TimeUnit.valueOf(cacheProperty.getType()))
                // 初始化缓存空间大小
                .initialCapacity(cacheProperty.getInitialCapacity())
                // 最大缓存数
                .maximumSize(cacheProperty.getMaximumSize())
                // 关上 value 的弱援用
                .weakValues()
                // 关上 key 的弱援用
                .weakKeys()
                .build();}


}
## resources 上面新建 META-INA 文件夹 新建 spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  org.redorblack.CacheAutoConfiguration

一个简略的 starter 就实现了

  • 能够不写 spring.factories
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
//
//@Import(CacheAutoConfiguration.class)
// 导入主动注入配置类  能够不写 spring.factories 文件
@ImportAutoConfiguration(CacheAutoConfiguration.class)
public @interface EnableCaffeineCache {}
@Configuration
@EnableConfigurationProperties(CacheProperty.class)
public class CacheAutoConfiguration {

    @Bean
    public Cache<String, Object> creatCaffeineCache(CacheProperty cacheProperty) {return Caffeine.newBuilder()
                // 设置最初一次写入或拜访后通过固定工夫过期
                .expireAfterWrite(cacheProperty.getDuration(), TimeUnit.valueOf(cacheProperty.getType()))
                // 初始化缓存空间大小
                .initialCapacity(cacheProperty.getInitialCapacity())
                // 最大缓存数
                .maximumSize(cacheProperty.getMaximumSize())
                // 关上 value 的弱援用
                .weakValues()
                // 关上 key 的弱援用
                .weakKeys()
                .build();}


}

这样也能够,应用注解也能够实现

  • 还有种 EnableXXX,EnableSync 这种注解, 用的 importSelector 这种导入形式
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
// 引入选择器
@Import(CaffeineCacheSelector.class)
public @interface EnableCaffeineCache {


    /**
     * 默认开关
     *
     * @return
     */
    boolean enable() default false;}
public class CaffeineCacheSelector implements ImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        Class<?> annotationType = EnableCaffeineCache.class;
        AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(annotationType.getName(), false));
        Boolean enable;
        if (attributes.getBoolean("enable")) enable = true;
        else enable = false;
        if (enable) {return new String[]{CacheAutoConfiguration.class.getName()};
        }
        return new String[0];
    }
@Configuration
// 扫描 ioc 的 bean 能够扫描第三方包
@ComponentScan("org.redorblack.*")
public class CacheAutoConfiguration {
    @Bean
    public Cache<String, Object> creatCaffeineCache(CacheProperty cacheProperty) {System.out.println("曾经启动了 ------------------");
        return Caffeine.newBuilder()
                // 设置最初一次写入或拜访后通过固定工夫过期
                .expireAfterWrite(cacheProperty.getDuration(), TimeUnit.valueOf(cacheProperty.getType()))
                // 初始化缓存空间大小
                .initialCapacity(cacheProperty.getInitialCapacity())
                // 最大缓存数
                .maximumSize(cacheProperty.getMaximumSize())
                // 关上 value 的弱援用
                .weakValues()
                // 关上 key 的弱援用
                .weakKeys()
                .build();}
正文完
 0