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