spring Aop中aop:advisor 与 aop:aspect的区别


在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 {

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值