几条原则:
- If the class of a target object that is to be proxied (hereafter simply referred to as the target class) doesn't implement any interfaces, then a CGLIB-based proxy will be created.【原因是:This is the easiest scenario, because JDK proxies are interface based, and no interfaces means JDK proxying isn't even possible. 】
- If the
proxyTargetClassproperty of theProxyFactoryBeanhas been set totrue, then a CGLIB-based proxy will be created. 【无视ProxyFactoryBean的属性proxyInterfaces是否有设置成为某一个或多个接口名】 - If the
proxyInterfacesproperty of theProxyFactoryBeanhas been set to one or more fully qualified interface names, then a JDK-based proxy will be created.
- If the
proxyInterfacesproperty of theProxyFactoryBeanhas not been set but the target class does implement one (or more) interfaces, then theProxyFactoryBeanwill auto-detect the fact that the target class does actually implement at least one interface, and a JDK-based proxy will be created, or a CGLIB-based proxy will be created.【如果是JDK代理这一条和第三条行为一致】
本文详细介绍了Spring AOP中代理对象的创建机制。主要包括:当目标对象未实现任何接口时,会使用CGLIB代理;如果目标类实现了至少一个接口且没有明确指定代理接口,则会自动使用JDK动态代理;当明确设置了代理接口属性时,无论目标类是否实现接口,都将使用JDK动态代理;最后,如果配置了proxyTargetClass为true,将强制使用CGLIB代理。
629

被折叠的 条评论
为什么被折叠?



