背景:项目中使用了注解加切面实现某些数据接口的缓存,想把项目中使用了缓存的接口及信息可视化,便于管理查看。这个功能实现的话大概就是解析接口方法上的注解,加入统一管理的集合信息中。
但是一般程序中只有访问到方法的时候才能主动做解析,而且多次访问会重复解析,所以考虑在项目启动时就遍历Bean解析这些注解的方法。
为了实现上面这个功能,就使用了BeanPostProcessor接口。
Spring中的BeanPostProcessor在实例化过程处于的位置分是前置处理和后置处理,对应的BeanPostProcessor 接口提供了两个可实现的方法:postProcessBeforeInitialization和postProcessAfterInitialization。
前者在实例化及依赖注入完成后、在任何初始化代码(比如配置文件中的init-method)调用之前调用,后者在初始化代码调用之后调用。此处需要注意的是
- 接口中的两个方法都要将bean返回,不能返回null,如果返回的是null那么我们通过getBean()方法将得不到目标。
- BeanPostProcessor本身也是一个Bean,一般而言其实例化时机要早过普通的Bean,单也有特殊的时候需要自己保证在目标bean之前初始化。
public interface BeanPostProcessor {
/**在任何bean初始化回调(如InitializingBean的afterPropertiesSet或自定义初始化方法)之前,将此BeanPostProcessor应用于给定的新bean实例.bean将已填充有属性值。 返回的 bean 实例可能是原始实例的包装器
*/
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
/**
* 在任何bean初始化回调(如InitializingBean的afterPropertiesSet或自定义初始化方法)之后,将此BeanPostProcessor应用于给定的新 bean 实例。bean 将已填充有属性值。 返回的 bean 实例可能是原始实例的包装器。在 FactoryBean 的情况下,将为 FactoryBean 实例和由 FactoryBean 创建的对象(从 Spring 2.0 开始)调用此回调。 后处理器可以通过相应的bean instanceof FactoryBean检查来决定是应用于 FactoryBean 或创建的对象还是两者。
*/
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
public class CollectCache implements ApplicationListener<ContextRefreshedEvent> ,BeanPostProcessor{
private List<CacheInfo> cacheInfoList = new ArrayList<>();
/**
* 通过postProcessAfterInitialization方法 在每个bean初始化过程中解析注解信息存到cacheInfoList集合中
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Method[] methods = bean.getClass().getDeclaredMethods();
for (Method method : methods) {
//获取目标注解 填充注解的属性信息放到cacheInfoList集合中
GetCache getCache = AnnotationUtils.findAnnotation(method, GetCache.class);
if (getCache != null) {
CacheInfo cacheInfo = new CacheInfo();
String prefix = getCache.prefix();
String resume = getCache.resume();
String desc = getCache.desc();
cacheInfo.setPrefix(prefix);
cacheInfo.setResume(resume);
cacheInfo.setDesc(desc);
cacheInfoList.add(cacheInfo);
}
}
return bean;
}
/**
* 通过onApplicationEvent在应用启动完成后把缓存信息集合cacheInfoList持久化存下来
*/
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
ApplicationContext context = contextRefreshedEvent.getApplicationContext();
if (!CollectionUtils.isEmpty(cacheInfoList)) {
String key = "cacheInfoList";
context.getBean(RedisTemplate.class).opsForValue().set(key, cacheInfoList, 365, TimeUnit.DAYS);
}
}
}