Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write code to intercept internal calls

Shulou Source: shulou.com Published: 2022-06-03 07:14:30 09月15日 Update

This article mainly introduces "how to write code to intercept internal calls". In daily operations, I believe many people have doubts about how to write code to intercept internal calls. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to write code to intercept internal calls". Next, please follow the editor to study!

The following is the native writing of CGLib (using the class implementation in the net.sf.cglib.proxy.* package)

Class Foo {public void fun1 () {System.out.println ("fun1"); fun2 ();} public void fun2 () {System.out.println ("fun2");}} class CGlibProxyEnhancer implements MethodInterceptor {public Object getProxy (Class clazz) {Enhancer enhancer = new Enhancer (); enhancer.setSuperclass (clazz); enhancer.setCallback (this); return enhancer.create () } @ Overridepublic Object intercept (Object obj, Method method, Object [] args, MethodProxy proxy) throws Throwable {System.out.print ("before"); Object result = proxy.invokeSuper (obj,args); return result;}} public class Test {public static void main (String [] args) {CGlibProxyEnhancer pf = new CGlibProxyEnhancer (); Foo foo = (Foo) pf.getProxy (Foo.class); foo.fun1 ();}}

The print result is:

Before fun1

Before fun2

You can see that although fun2 () is called through foo.fun1 (), fun () 2 can still be proxied.

But if you use the basic way of writing Spring AOP:

Class Foo {public void fun1 () {System.out.println ("fun1"); fun2 ();} public void fun2 () {System.out.println ("fun2");}} class Before implements MethodBeforeAdvice {public void before (Method method, Object [] objects, Object o) throws Throwable {System.out.print ("before");}} public class TestCGLib {public static void main (String [] args) {Foo foo = new Foo (); BeforeAdvice advice = new Before () ProxyFactory pf = new ProxyFactory (); pf.setOptimize (true); / / enable Cglib2AopProxy creation agent pf.setProxyTargetClass (true); pf.setTarget (foo); pf.addAdvice (advice); Foo proxy = (Foo) pf.getProxy (); proxy.fun1 ();}}

The output is as follows:

Before fun1

Fun2

It can be seen that the fun2 method is not proxied.

This post also talked about how to change the fun2 in cglib to private, and the final result is the same as that of regular AOP.

At this point, the study on "how to write code to intercept internal calls" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Between code learning results agents writing methods more help practical next posts general articles theories knowledge articles websites materials follow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology Shulou Tech Info Redmi Apple