Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of not getting native object annotations caused by Spring proxy object

Shulou Source: shulou.com Published: 2022-06-01 01:40:29 09月21日 Update

In this article, the editor introduces in detail "how to solve the problem of not getting native object annotations caused by Spring proxy object". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to solve the problem of not getting native object annotations caused by Spring proxy objects" can help you solve your doubts.

Problem description

When I accept mq messages, I need to make a limit on the number of retries. If I exceed the maxNum, I will send an email alarm and will not try again.

So I need to proxy the consumer object, and then if I exceed the number of exceptions, I directly return success and send a success message, but the way I get the consumer handler method is through method.getAnnotation (XXClient.class), then I return null.

Question sample code

Target class, I will define a test method here, in which to do some simple printing.

@ Componentpublic class TestBean {@ Anno public void test () {System.out.println ("test.");}}

The main purpose of proxy logic processing is to do a @ Around method override to ensure that the logic I inserted is output before calling the target method.

@ Target ({ElementType.METHOD}) @ Retention (RetentionPolicy.RUNTIME) public @ interface Anno {String key () default;} @ Aspect@Componentpublic class AnnoAspect {@ Around ("@ annotation (anno)") public Object anno (ProceedingJoinPoint point, Anno anno) throws Throwable {System.out.println ("anno invoke!"); return point.proceed ();}}

Call point, get bean through AnnotationConfigApplicationContext. Then get all the methods through getMethods (), and finally find the Method object annotated by Anno.

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext (BeanStart.class); TestBean bean = applicationContext.getBean (TestBean.class); Class

Tags: Objects methods agents annotations code articles logic questions success content methods times times messages goals processing rigor propriety information that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Technology Microsoft macOS MySQL