Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report