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>
3345

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



