之前是找了各种AOP的博文看了好多,现在上官网啃啃英文文档(终究是躲不过的),把关键的记录下来做笔记。
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#spring-core
##5.8. Proxying Mechanisms机制
Spring AOP uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. JDK dynamic proxies are built into the JDK, whereas CGLIB is a common open-source class definition library (repackaged into spring-core).
If the target object to be proxied implements at least one interface, a JDK dynamic proxy is used. All of the interfaces implemented by the target type are proxied. If the target object does not implement any interfaces, a CGLIB proxy is created.
JDK 的动态代理要求代理者必须实现接口 , 否则不能生成代理对象
If you want to force the use of CGLIB proxying (for example, to proxy every method defined for the target object, not only those implemented by its interfaces), you can do so. However, you should consider the following issues:
With CGLIB, final methods cannot be advised, as they cannot be overridden in runtime-generated subclasses.
由于cglib会继承被代理类,需要重写被代理方法,所以被代理类不能是final类,被代理方法不能是final
As of Spring 4.0, the constructor of your proxied object is NOT called twice anymore, since the CGLIB proxy instance is created through Objenesis. Only if your JVM does not allow for constructor bypassing, you might see double invocations and corresponding debug log entries from Spring’s AOP support.
JDK动态代理和Cglib代理,Cglib代理可以配置强制使用的,方法是:
To force the use of CGLIB proxies, set the value of the proxy-target-class attribute of the aop:config element to true, as follows:知道可以配置就行了,如果忘记了配置细节,上官网查。