Spring aop标签的使用

笔者花了一点时间进行了关于spring aop标签的使用,希望能帮助初学spring框架的童鞋们。

只列举了最重要的部分。供参考

1.定义切面类
package com.lm.aop.xml;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class XmlHandler {
    //前置通知
    public void before(JoinPoint point){
        System.out.println(point.getSignature().getName()+"执行之前,before....");
    }
    //后置通知
    public void after(JoinPoint point){
        System.out.println(point.getSignature().getName()+"执行之前,after....");
    }
    //方法返回后通知
    public void returnafter(JoinPoint point){
        System.out.println(point.getSignature().getName()+"正常执行之后,returnafter....");
    }
    //环绕通知
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println(pjp.getSignature().getName()+"执行之前,around....");
        Object proceed = pjp.proceed(); //执行目标对象的方法
        System.out.println(pjp.getSignature().getName()+"执行之后,around....");
        return proceed;
    }
    //异常通知
    public void throwingException(JoinPoint point,Exception ex){
        System.out.println(point.getSignature().getName()+"发生异常"+ex.getMessage());
    }
}

2.Spring Xml配置(Aop标签)
<? xml version = "1.0" encoding = "UTF-8" ?>
      xmlns:aop = " http://www.springframework.org/schema/aop" ;
      xsi:schemaLocation = " http://www.springframework.org/schema/beans
      <!--配置account对象 -->
      < bean name = "account" class = "com.lm.aop.bean.Account" >
           < property name = "id" value = "1" ></ property >
           < property name = "name" value = "LeeSin" ></ property >
           < property name = "balance" value = "5000" ></ property >
      </ bean >
      <!--配置 dao 层 -->
      < bean name = "dao" class = "com.lm.aop.dao.AccountDaoImpl" ></ bean >
      <!-- 配置service -->
      < bean name = "service" class = "com.lm.aop.service.AccountServiceImpl" >
           < property name = "dao" ref = "dao" ></ property >
           < property name = "fromAccount" ref = "account" ></ property >
      </ bean >
      <!--配置切面类对象 -->
      < bean name = "handler" class = "com.lm.aop.xml.XmlHandler" ></ bean >
      <!-- aopConfig配置代理对象 -->
      < aop:config >
       <!-- execution(修饰符(可以不写) 参数类型(必须写可以用*代替) 包名+类名+方法名(可以用*代替(..)   ..的意思是代表参数可有可无。   )) -->
           < aop:pointcut expression = "execution(* com.briup.aop.service.*.*(..))"
               id = "myPointCut" />
           < aop:aspect id = "aspect" ref = "handler" >
               < aop:before method = "before" pointcut-ref = "myPointCut" />
               < aop:after method = "after" pointcut-ref = "myPointCut" />
               < aop:around method = "around" pointcut-ref = "myPointCut" />
           </ aop:aspect >
      </ aop:config >
</ beans >


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值