您的位置:首页 > 编程语言 > ASP

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 {

    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  aop