AOP Proxies

Spring AOP 默认使用标准 JDK 动态代理进行 AOP 代理。这使任何界面(或一组界面)都可以被代理。

Spring AOP defaults to using standard JDK dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be proxied.

Spring AOP 还可以使用 CGLIB 代理。这是代理类而非界面的必要条件。默认情况下,如果业务对象未实现一个界面,则使用 CGLIB。由于根据界面而非类进行编程是一种好习惯,因此业务类通常实现一个或多个业务界面。在这些(希望很少见)情况下,你可以强制使用 CGLIB,比如你需要对未在界面上声明的方法进行切面处理,或需要以具体类型将代理对象作为参数传递给方法。

Spring AOP can also use CGLIB proxies. This is necessary to proxy classes rather than interfaces. By default, CGLIB is used if a business object does not implement an interface. As it is good practice to program to interfaces rather than classes, business classes normally implement one or more business interfaces. It is possible to force the use of CGLIB, in those (hopefully rare) cases where you need to advise a method that is not declared on an interface or where you need to pass a proxied object to a method as a concrete type.

务必要掌握 Spring AOP 基于代理的事实。有关此实现详细信息的实际含义,请参阅 Understanding AOP Proxies 进行深入了解。

It is important to grasp the fact that Spring AOP is proxy-based. See Understanding AOP Proxies for a thorough examination of exactly what this implementation detail actually means.