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

The solution that Spring AOP does not work on nesting methods

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you the solution that Spring AOP does not work on nesting methods, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Spring AOP does not work with nesting methods

Today, when investigating the logging of system operations, many tutorials are implemented with the help of Spring AOP mechanism. Therefore, this method is also used to realize it. Comment the custom pointcut on the delete log method in Service, but the execution does not take effect.

The code is as follows:

/ / attempt to delete overflow log public synchronized void tryDelOverflowLog () {logNum++; if (logNum-LogConst.MAX_NUM > 0) {int delNum = logNum-LogConst.MAX_NUM + LogConst.EXTRA_NUM; logNum-= delNum; removeOverflowLog (delNum) }} / / after the log overflow, delete the newly loaded log @ ServiceLog (type = LogConst.TYPE_LOG_RECORD, description = "Operation log cache overflow, system automatically empties the cache") public void removeOverflowLog (int delNum) {custLogMapper.removeOverflowLog (delNum);}

When using Spring AOP, the Service Bean objects we get from the IOC container are actually proxy objects, not the Service Bean objects themselves, that is, they are not proxied objects or proxy targets. When I use the this keyword to nest other methods in my Service class, because the this keyword does not refer to the proxy object of the Service Bean object, but to itself, Spring AOP cannot intercept these nested methods.

To solve this problem.

The easiest way is to inject yourself into yourself and call this method with the injected itself. Or you can use aspectj weaving instead of spring aop, which can solve the problem by bottoming out. What I use is to inject myself into myself.

/ * solve the problem that Spring AOP nested calls do not take effect by injecting itself * / @ Autowired private ApplicationContext applicationContext; private LogService self; @ PostConstruct private void init () {self = (LogService) applicationContext.getBean ("logService");} / / attempt to delete overflow log public synchronized void tryDelOverflowLog () {logNum++ If (logNum-LogConst.MAX_NUM > 0) {int delNum = logNum-LogConst.MAX_NUM + LogConst.EXTRA_NUM; logNum-= delNum; self.removeOverflowLog (delNum) }} Spring AOP, nested invocation invalidation and solution add the annotation @ EnableAspectJAutoProxy (proxyTargetClass = true, exposeProxy = true) to get the current proxy interface public interface ICurrentAopProxyService {default T getCurrentProxyService () {return (T) AopContext.currentProxy ();}} requires a nested calling Service to implement it

When calling, rewrite the code public SysMerchantVersion selectByMerchantId (Long merchantId) {return getCurrentProxyService (). GetOne (new QueryWrapper () .lambda () .eq (SysMerchantVersion::getMerchantId, merchantId));} the above is the solution that Spring AOP does not work on nested methods. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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