- 例如 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();
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。