On the AOP (two) - simple application

Foreplay " On the AOP (a) - the basic concept of" ready pre-understanding.

Benpian then analyze what AOP.

SpringAOP underlying technology

JDK dynamic proxies and dynamic proxy CGLIB

Both are at run time weaving.

JDK dynamic proxy is based on the aggregation interface, because java is single inheritance, the proxy object has inherited the Proxy, can not inherit more, so the interface can only achieve the target object. In the Proxy class among first instantiate an object ProxyClassFactory, then call the apply method in the get method, to complete the creation of the proxy class.

CGLIB dynamic proxy it based inheritance, with very bottom CGLIB byte code technology, subclass of a class, and the method call interception techniques to intercept all the parent class method in the subclass, and the respective local woven Intercepting the crosscutting logic.

Spring AOP applications

In surround notice:

 

Around advice:
 

ProceedingJoinPoint is JoinPoint enhanced classes, Proceedingjoinpoint inherit JoinPoint, expanded JoinPoint.

JoinPoint not invoke method, not reflected; ProceedingJoinPoint class it is necessary to proceed () method, and proceed () parameters may also be modified.

Test code:

dao:


 
Test categories:

 

Changing the input parameters.

In the actual application environment, the section generally single embodiment.

AspectJ support


 

AspectJ support a very important point!

This raises a question:

AOP and AspectJ and SpringAOP What is the relationship?

AOP is a programming idea, but SpringAOP is an implementation of AOP! AspectJ AOP can be achieved there and JBoss AOP.

Why SpringAOP to use AspectJ?

In Spring2.0 own set of AOP syntax, but very complex, the outside world does not accept.

So the follow-up Spring AspectJ syntax help, just help the AspectJ syntax, or the underlying Spring underlying technology.

to sum up:

Spring AOP在底层就是利用JDK动态代理或CGLib动态代理技术为目标Bean织入横切逻辑。

CGLIB是通过继承来操作子类的字节码生成代理类,JDK是通过接口,然后利用java反射完成对类的动态创建,严格意义上来说CGLIB的效率高于JDK的反射,但是这种效率取决于代码功力,其实可以忽略不计,毕竟JDK是JVM的亲儿子。

AOP深入内容非常多,博主暂时只能浅析到这里,后续继续进行学习记录总结。

参考资料:

Spring学习之Spring AOP的底层实现技术 -- 动态代理


本人才疏学浅,以上纯属个人理解,如有不对,还望批评指正。

Guess you like

Origin www.cnblogs.com/vince-zww/p/11504175.html