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 passing through the proxy class when calling inside SpringAop

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

Share

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

How to solve the problem of calling SpringAop without proxy class? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

SpringAopAOP agent weaving period

Compile-time weaving-aspectj framework

Class loading time weaving-aspectj framework

Runtime weaving-spring-aop

Dynamic agent mode

JDK-the proxied object must implement the interface

CGLIB-implements the proxy function by inheriting the proxied object

Solution SpringAop internal calls without going through the proxy class (but through this) scenario 1

Weaving code through compile time weaving or class loading time weaving

Option 2

Call the target method through the current proxy class

The direct call to getAll () in the getOne () method is through the this object, so the @ AopLog on the getAll () method will not be scanned by AOP.

@ Service@Slf4jpublic class ServiceImpl implements IService {@ Override @ AopLog ("value=getOne") public void getOne () {log.info ("getOne running"); / / directly call the getAll () method = this.getAll () getAll ();} @ Override @ AopLog ("value=getAll") public void getAll () {log.info ("getAll running");}}

Modify the getOne () method, get the current proxy class through the AopContext.currentProxy () method, and call the getAll () method through the proxy class, which is called through the proxy class.

@ Service@Slf4jpublic class ServiceImpl implements IService {@ Override @ AopLog ("value=getOne") public void getOne () {log.info ("getOne running"); / / get the current proxy class and call the getAll () method ((IService) AopContext.currentProxy ()). GetAll () through the proxy class;} @ Override @ AopLog ("value=getAll") public void getAll () {log.info ("getAll running") } @ AopLog ("value=getById") private void getById () {log.info ("getById running");}} description of the riddle of this invalidating SpringAop

Class Demo is scanned by AOP, in which there are two methods An and B. Method B is called in method A, and the proxy of method B does not take effect when method An is executed.

Problem analysis

We know that the bottom layer of AOP uses JDK dynamic proxy and cglib dynamic proxy to create the corresponding proxy object through judgment.

No matter which method, after the final execution of the agent, the target method will be executed: method.invoke (target,agrs)-- > pass in the target object

So after executing the agent of method A, execute the target method of A, and the object executed at this time is the target object, so the target object executing method An is the this hidden in A.

It refers to the target object, that is, the person who executes the B method is no longer the proxy object but the target object, so the B method will not be proxied.

This is the answer to the question about how to solve the problem of internal calls to SpringAop without going through the proxy class. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.

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