简体   繁体   中英

How does a Pointcut in Spring framework work internally

I am just wondering, How does a Pointcut in Spring framework work internally. How does it get to know the what method will be executed next (and then it does its job before/after the method executes)? Does it use reflection somehow? Even if I put a debug point at the Pointcut definition, debugger doesn't hit it.

It is realized by proxying your aspected method. A call to your method is replaced by a call to the proxy method. The proxied method executes first the before-Annotations, then calls your original method, and then executes the after-Annotations.

The proxy method is syntetic, it does nowhere appear in your code, so you cannot add a breakpoint.

Well what I understand is that it's a process of weaving. If you consider your pointcuts are nothing but signatures, they are flexible to choose from package,interface,class or methods or even wildcards.

When your application loads Spring sort of creates the proxies of all the targets , That's why in my opinion it's not a good idea to use wildcard, instead apply on specific targets. The process of creating proxies on targets are actually weaving.

The very reason debugger doesn't work is because the class isn't being invoked, it's the proxies created by Spring. and yes Spring extensively uses reflection.

I once tried to put Aspects on JAX-RS classes and it failed, because the proxies created by Spring was not being taken into consideration by JAX-RS resources and advice wasn't being fired.

Also I used wild card and then in any stack trace in any exception, i could see the Spring proxy call somewhere in the stack trace.

PS it's more of observation than knowledge.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM