spring Aop中aop:advisor 与 aop:aspect的区别
2016-10-21 23:34
1166 查看
在spring的配置中,会用到这两个标签.那么他们的区别是什么呢?
<bean id="testAdvice" class="com.myspring.app.aop.MyAdvice"/> //切面代码
使用<aop:aspect>配置时,
如果切面代码是自动注入的bean,那么<aop:aspect>的ref属性直接写bean的注入名字就可以了!
<aop:config>
<aop:aspect ref="testAdvice" id="testAspect">
<aop:pointcut expression="(execution(* com.myspring.app.aop.TestPoint.*(..)))" id="testPointcut"/>
<aop:before method="doBefore" pointcut-ref="testPointcut"/>
</aop:aspect>
</aop:config>
<aop:config>
<aop:pointcut expression="(execution(* com.myspring.app.aop.TestPoint.*(..)))" id="mypoint"/>
<aop:advisor advice-ref="testAdvice" pointcut-ref="mypoint"/>
</aop:config>
注意:2种格式的书写次序.
=========================================================================
package com.myspring.app.aop;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.springframework.aop.MethodBeforeAdvice;
/**
* 方法前置通知
* @author Michael
*
*/
@Component("myAdvice")//如果是自动装配,在定义切面的时候直接写在ref属性里就可以了
public class MyAdvice implements MethodBeforeAdvice{
//如果使用aop:advisor配置,那么切面逻辑必须要实现advice接口才行!否则会失败!
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("前置通知");
}
//如果是<aop:aspect>配置,编写一般的方法就可以了,然后在切面配置中指定具体的方法名称!
public void doBefore(JoinPoint point) throws Throwable {
}
}
<bean id="testAdvice" class="com.myspring.app.aop.MyAdvice"/> //切面代码
使用<aop:aspect>配置时,
如果切面代码是自动注入的bean,那么<aop:aspect>的ref属性直接写bean的注入名字就可以了!
<aop:config>
<aop:aspect ref="testAdvice" id="testAspect">
<aop:pointcut expression="(execution(* com.myspring.app.aop.TestPoint.*(..)))" id="testPointcut"/>
<aop:before method="doBefore" pointcut-ref="testPointcut"/>
</aop:aspect>
</aop:config>
<aop:config>
<aop:pointcut expression="(execution(* com.myspring.app.aop.TestPoint.*(..)))" id="mypoint"/>
<aop:advisor advice-ref="testAdvice" pointcut-ref="mypoint"/>
</aop:config>
注意:2种格式的书写次序.
=========================================================================
package com.myspring.app.aop;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.springframework.aop.MethodBeforeAdvice;
/**
* 方法前置通知
* @author Michael
*
*/
@Component("myAdvice")//如果是自动装配,在定义切面的时候直接写在ref属性里就可以了
public class MyAdvice implements MethodBeforeAdvice{
//如果使用aop:advisor配置,那么切面逻辑必须要实现advice接口才行!否则会失败!
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("前置通知");
}
//如果是<aop:aspect>配置,编写一般的方法就可以了,然后在切面配置中指定具体的方法名称!
public void doBefore(JoinPoint point) throws Throwable {
}
}
相关文章推荐
- spring Aop中aop:advisor 与 aop:aspect的区别 【爱玩酷java php 学习路径】
- Spring AOP Aspect Adivisor区别
- spring 中的<aop:advisor>和<aop:aspect>有什么区别?
- spring的aop:advisor和aop:aspect
- Spring AOP高级——源码实现(2)Spring AOP中通知器(Advisor)与切面(Aspect)
- Spring AOP 之 RegexpMethodPointcutAdvisor
- Spring Aop Annotation @Aspect
- spring aop实现过程之三Spring AOP中Aspect编织的实现
- Spring 嵌套AOP 的顺序问题 指定Aspect优先级
- Spring2.5 注解 Aspect AOP
- Spring_AOP_Annotation使用Aspect实现动态代理
- 让spring3.0可以使用aop注解的方法是导入两个jar包aspectjrt.jar aspectjweaver.jar
- Spring AOP:使用NameMatchMethodPointcutAdvisor实现切面编程
- Spring_AOP_XML使用Aspect实现动态代理(常用) .
- Spring AOP AspectJExpressionPointcutAdvisor
- spring面向切面编程AOP(Aspect-orented programming)
- Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法
- spring对AOP的支持 JDK动态代理和CGLIB的区别
- spring.net、castle windsor、unity实现aop、ioc的方式和简单区别
- spring AOP @Around @Before @After 区别