SpringAOP

一.AOP术语

target–目标类,需要被代理的类,如UserService
JointPoint–连接点,目标类中的所有方法
PointCut–切入点,已经被增强的连接点,如addUser()
Advice–通知
Aspect–切面(目标类以外的类)
Weaving–织入,把通知应用到目标类来创建代理对象的过程

二.aop实例

1.五种类型的通知

1,前置通知(before)
    在目标方法执行之前调用,如果通知抛出异常,则目标方法不再执行
2,后置通知(aferReturning)
    在目标方法正常执行后调用,如果目标方法抛出异常,则不再执行后置通知
3,环绕通知(around)
    在目标方法执行前后分别执行,可以阻止目标方法的执行
4,抛出异常通知(afterThrowing)
    目标方法抛出异常后执行,如果目标方法没有抛出异常,则不执行异常通知
5,最终通知(after)
    目标方法执行之后调用,无论目标方法是否异常都会调用

2.xml配置

2.1 切面类和通知方法

public class Transtion {

    //前置通知
    public void beforeNotify(JoinPoint joinPoint){
        System.out.println("连接点 = "+joinPoint.getSignature().getName());
        System.out.println("目标方法 = "+joinPoint.getTarget());
        System.out.println("beforeNotify");
    }

    //后置通知
    public void afterReturningNotify(JoinPoint joinPoint,Object val){
        System.out.println("返回值 = "+val);
        System.out.println("afterReturningNotify");
    }

    //环绕通知(参数不能少)
    public Object arroundNotify(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{

        System.out.println("arroundNotify before");
        Object object = proceedingJoinPoint.proceed();
        System.out.println("arroundNotify after");
        return object;

    }

    //抛出异常通知

    public void afterThrowingNotify(JoinPoint joinPoint){

        System.out.println("afterThrowingNotify");
    }

    //最终通知
    public void afterNotify(JoinPoint joinPoint){
        System.out.println("afterNotify");
    }
}

2.2.xml配置通知

 <bean id="personDao" 
    class="com.spring.aop.xml.transtion.PersonDaoImpl"></bean>
<bean id="transtion"
    class="com.spring.aop.xml.transtion.Transtion"></bean> 
<aop:config>
    <!-- 切入点 表达式,确定目标类-->
    <aop:pointcut 
      expression="execution(* com.spring.aop.xml.transtion.PersonDaoImpl.*(..))" 
      id="perform"/>        
    <!-- 切面 ref 指向切面 -->
    <aop:aspect ref="transtion">
        <!-- 前置通知 ref 指向目标类
        <aop:before method="beforeNotify" pointcut-ref="perform"/>
        --> 
        <!-- 后置通知  
        ref 指向目标类
        returning 目标方法返回值,名称需和方法的参数名保持一致
        <aop:after-returning method="afterReturningNotify" pointcut-ref="perform" returning="val"/>
        -->
        <!-- 环绕通知
        <aop:around method="arroundNotify" pointcut-ref="perform"/>
        --> 
        <!-- 抛出异常通知 
        throwing:异常信息,名称需要和方法中的参数名保持一致
        <aop:after-throwing method="afterThrowingNotify" pointcut-ref="perform" throwing="e"/>
        --> 
        <!-- 最终通知-->
        <aop:after method="afterNotify" pointcut-ref="perform"/>     
        </aop:aspect>
      </aop:config>

3.注解配置

3.1 总的xml文件中需要配置如下信息

<!-- 扫描注解类 -->
<context:component-scan base-package="com.spring.aop.aspect"></context:component-scan>

<!-- 确定aop注解生效 -->
<aop:aspectj-autoproxy/>

3.2 用到的bean类需要加注解

@Service("personDao")
public class PersonDaoImpl implements PersonDao{
....
}

@Component
@Aspect----声明切面类
public class Transtion {
...
}

3.3 注解的通知

public class Transtion {

    //声明公共切入点
    @Pointcut("execution (* com.spring.aop.aspect.PersonDaoImpl.*(..))")
    private void myPointCut(){

    }

    //前置通知
//  @Before("myPointCut()")
    public void beforeNotify(JoinPoint joinPoint){
        System.out.println("连接点 = "+joinPoint.getSignature().getName());
        System.out.println("目标方法 = "+joinPoint.getTarget());
        System.out.println("beforeNotify");
    }

    //后置通知
//  @AfterReturning(value="myPointCut()",returning="val")
    public void afterReturningNotify(JoinPoint joinPoint,Object val){
        System.out.println("返回值 = "+val);
        System.out.println("afterReturningNotify");
    }

    //环绕通知(参数不能少)
//  @Around(value="myPointCut()")
    public Object arroundNotify(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{

        System.out.println("arroundNotify before");
        Object object = proceedingJoinPoint.proceed();
        System.out.println("arroundNotify after");
        return object;

    }

    //抛出异常通知
//  @AfterThrowing(value="myPointCut()",throwing="e")
    public void afterThrowingNotify(JoinPoint joinPoint,Throwable e){

        System.out.println("afterThrowingNotify = "+e.getMessage());
    }

    //最终通知
    @After("myPointCut()")
    public void afterNotify(JoinPoint joinPoint){
        System.out.println("afterNotify");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值