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 write code to intercept internal calls

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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!

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