spring对AOP的支持(JDK的动态代理实现AOP和CGLIB实现AOP)
2017-04-10 17:53
846 查看
1、如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP
2、如果目标对象实现了接口,可以强制使用CGLIB实现AOP
3、如果目标对象没有实现了接口,必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换
如何强制使用CGLIB实现AOP?
* 添加CGLIB库,SPRING_HOME/cglib/*.jar
* 在spring配置文件中加入<aop:aspectj-autoproxy proxy-target-class="true"/>
JDK动态代理和CGLIB字节码生成的区别?
* JDK动态代理只能对实现了接口的类生成代理,而不能针对类
* CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法
因为是继承,所以该类或方法最好不要声明成final
2、如果目标对象实现了接口,可以强制使用CGLIB实现AOP
3、如果目标对象没有实现了接口,必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换
如何强制使用CGLIB实现AOP?
* 添加CGLIB库,SPRING_HOME/cglib/*.jar
* 在spring配置文件中加入<aop:aspectj-autoproxy proxy-target-class="true"/>
JDK动态代理和CGLIB字节码生成的区别?
* JDK动态代理只能对实现了接口的类生成代理,而不能针对类
* CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法
因为是继承,所以该类或方法最好不要声明成final
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <aop:aspectj-autoproxy proxy-target-class="true"/>//强制使用CGLIB实现AOP <bean id="securityHandler" class="com.bjsxt.spring.SecurityHandler"/> <bean id="userManager" class="com.bjsxt.spring.UserManagerImpl"/> <aop:config> <aop:aspect id="security" ref="securityHandler"> <aop:pointcut id="allAddMethod" expression="execution(* add*(..))"/> <aop:before method="checkSecurity" pointcut-ref="allAddMethod"/> </aop:aspect> </aop:config> </beans>
相关文章推荐
- Spring(十)通过动态代理(JDK的Proxy)和cglib实现AOP技术
- spring(AOP)静态代理、JDK动态代理、cglib实现代理
- 静态代理、JDK动态代理、CGLIB动态代理、Spring实现AOP、IOC+AOP
- Spring中AOP实现的两种方式之JDK和cglib的动态代理
- Spring AOP 代理实现的两种方式: JDK动态代理 和 Cglib框架动态代理
- Spring AOP之---基于JDK动态代理和CGLib动态代理的AOP实现
- Spring中AOP实现的两种方式之JDK和cglib的动态代理
- spring对AOP的支持 JDK动态代理和CGLIB的区别
- Spring系列之 (十一):AOP实现方式:动态代理的两个方式(JDK和Cglib)
- 通过CGLIB实现AOP的浅析(顺便简单对比了一下JDK的动态代理)
- 浅析Spring AOP源码(十三) jdk的动态代理和cglib的代理
- AOP实现--JDK中的动态代理和cglib代理
- AOP的底层实现-CGLIB动态代理和JDK动态代理
- 从JDK动态代理看Spring之AOP实现(转帖)
- JDK动态代理和CGLIB动态代理,实现Spring注解管理事务区别。
- Spring学习总结(二)——静态代理、JDK与CGLIB动态代理、AOP+IoC
- AOP的底层实现-CGLIB动态代理和JDK动态代理
- Spring AOP详解 、 JDK动态代理、CGLib动态代理
- Spring aop 基于JDK动态代理和CGLIB代理的原理以及为什么JDK代理需要基于接口
- Spring-JDK与CGlib代理实现AOP简单实例