将AspectJ AOP应用于注释时找不到注释

时间:2019-07-12 05:45:49

标签: java spring

在注释上使用外观时,不能使用AnnotationUtils.getAnnotation

//在这里,我找不到PulsarListener

PulsarListener annotation = AnnotationUtils.getAnnotation(method, PulsarListener.class);

当我删除@Aspect时,就可以了。

    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface PulsarListener {
        String[] topics() default {};
    }

    @Aspect
    @Slf4j
    @Component
    @Order(0)
    public class MDCAspect {
        @Around("@annotation(PulsarListener)")
        public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

            try {
                String requestUUID = MDC.get("requestUUID");
                if (StringUtils.isEmpty(requestUUID)) {
                    String uid = ObjectId.get().toHexString();
                    MDC.put("requestUUID", uid);
                }

                return joinPoint.proceed();

            } finally {
                MDC.clear();
            }

        }
    }

    @Component
    @Slf4j
    public class PulsarConsumer {

        @PulsarListener(topics = "${topics}")
        public void listen(Message<byte[]> receive) {
            //doSomething
        }

    }

    public class PulsarPostProcessor implements BeanPostProcessor {

        @Value("${pulsar.service.url}")
        private String pulsar_service_url;

        @Autowired
        private ApplicationContext applicationContext;

        @Override
        public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
            Method[] methods = bean.getClass().getDeclaredMethods();
            for (Method method : methods) {
                //here , i canot not found PulsarListener
                //here is the problem
                PulsarListener annotation = AnnotationUtils.getAnnotation(method, PulsarListener.class);
                if (annotation != null) {
                    if (log.isDebugEnabled()) {
                        log.debug("bean :{},method:{}", beanName, method.getName());
                    }
              }
         }
    }

0 个答案:

没有答案