Spring Aop增强
2016-09-23 19:38
316 查看
Spring AOP是纯JAVA代码实现的,不需要专门的编译过程和类加载器,在运行期间通过代理方式向目标类织入增强代码,扩展JAVA语言
AOP的底层原理:JDK的动态代理和CGLIB的动态代理
通知:是指拦截到JoinPoint之后所要做的事
AOP通知的类型
前置通知:
MethodBeforeAdvice
后置通知:
AfterReturningAdvice
环绕通知:
MethodInterceptor
异常抛出通知:ThrowsAdvice
引介通知:IntroductionInteceptor(很少使用)
切面类型
Advisor:
代表一般切面,Advice本身就是一个切面,对所有的方法都实现拦截
PointcutAdvisor:
代表带有切点的切面,可以指定拦截哪些方法
开发步骤:
第一步:引入jar包
aop和aopalliance
第二步:新建目标类HelloService和增强类HelloAdvice
第三步:配置
Advisor:
<bean id="helloAdvice" class="cn.itcast.springaop.HelloServiceBeforeAdvice"></bean>
<bean id="target" class="cn.itcast.springaop.HelloService" />
<bean id="helloService" class="org.springframework.aop.framework.ProxyFactoryBean" >
<property name="proxyInterfaces" value="cn.itcast.springaop.IHelloService" />
<!--如果不是针对接口代理,可以设置 <property name="proxyTargetClass" value="true"></property> ,将使用CGLib-->
<property name="interceptorNames" value="helloAdvice"></property>
<property name="target" ref="target"></property>
</bean>
第四步:
使用,注意使用getBean的时候,名称为增强后的名称,这里就是helloService而不是target
PointcutAdvisor:
<bean name="heloService" class="demo6.HelloService"></bean>
<bean name="helloAdvice" class="demo6.HelloAdvice"></bean>
<bean name="regexAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="pattern" value=".*add"></property>
<property name="advice" ref="helloAdvice"></property>
</bean>
<bean name="helloServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="heloService"></property>
<property name="interceptorNames" value="regexAdvisor"></property>
</bean>
AOP的底层原理:JDK的动态代理和CGLIB的动态代理
通知:是指拦截到JoinPoint之后所要做的事
AOP通知的类型
前置通知:
MethodBeforeAdvice
后置通知:
AfterReturningAdvice
环绕通知:
MethodInterceptor
异常抛出通知:ThrowsAdvice
引介通知:IntroductionInteceptor(很少使用)
切面类型
Advisor:
代表一般切面,Advice本身就是一个切面,对所有的方法都实现拦截
PointcutAdvisor:
代表带有切点的切面,可以指定拦截哪些方法
开发步骤:
第一步:引入jar包
aop和aopalliance
第二步:新建目标类HelloService和增强类HelloAdvice
第三步:配置
Advisor:
<bean id="helloAdvice" class="cn.itcast.springaop.HelloServiceBeforeAdvice"></bean>
<bean id="target" class="cn.itcast.springaop.HelloService" />
<bean id="helloService" class="org.springframework.aop.framework.ProxyFactoryBean" >
<property name="proxyInterfaces" value="cn.itcast.springaop.IHelloService" />
<!--如果不是针对接口代理,可以设置 <property name="proxyTargetClass" value="true"></property> ,将使用CGLib-->
<property name="interceptorNames" value="helloAdvice"></property>
<property name="target" ref="target"></property>
</bean>
第四步:
使用,注意使用getBean的时候,名称为增强后的名称,这里就是helloService而不是target
PointcutAdvisor:
<bean name="heloService" class="demo6.HelloService"></bean>
<bean name="helloAdvice" class="demo6.HelloAdvice"></bean>
<bean name="regexAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="pattern" value=".*add"></property>
<property name="advice" ref="helloAdvice"></property>
</bean>
<bean name="helloServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="heloService"></property>
<property name="interceptorNames" value="regexAdvisor"></property>
</bean>
相关文章推荐
- Spring Aop 实现函数内部增强
- Spring AOP(四)之After增强处理
- Spring AOP之增强介绍
- Spring AOP 增强类型
- Spring配置AOP实现定义切入点和织入增强
- Spring-AOP配置切入点方式及配置各种类型增强
- spring 声明式事务、异步调用、AOP灯增强类功能 失效问题
- AOP执行增强-Spring 源码系列(5)
- Intellij IDEA中Spring配置aop增强事务 出现BeanCreationException异常处理方法
- spring中的Cglib字节码增强实现AOP
- Spring AOP那些学术概念—通知、增强处理连接点(JoinPoint)切面(Aspect)
- Spring AOP(五)之Around增强处理
- Spring配置加事物加扫描dao层 加增强的AOP切面方法
- Spring AOP之增强(aspectj)
- spring AOP中的AfterThrowing增强处理不能完全处理异常
- Spring-AOP 增强(Advice)5种类型和创建增强类
- Spring-AOP 通过配置文件实现 前置增强
- Spring AOP--引入增强
- Spring AOP(4)- 异常增强
- Spring源码阅读(六)—AOP获取增强