- 自定义注解
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface BizRollback {
Class<? extends AbstractRollbackHandler> handler();
}
- 加载bean时处理自定义注解
@Component
public class CustomAnnotationProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Method[] methods = ReflectionUtils.getAllDeclaredMethods(bean.getClass());
if (methods != null) {
for (Method method : methods) {
BizRollback rollback = AnnotationUtils.findAnnotation(method, BizRollback.class);
if (rollback!=null){
LimitRollbackFactory.registerService(bean.getClass().getSimpleName(), method.getName(), rollback.handler());
}
}
}
return bean;
}
}