通知

//配置文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--目标对象--> <bean id="someService" class="cn.happy.ASPECTJXM.SomeSevice"></bean> <!--增强:通知--> <bean id="myAspectj" class="cn.happy.ASPECTJXM.MyAspectj"></bean> <!--aop--> <aop:config> <!-- <aop:pointcut id="mypoint" expression="execution(* *..ASPECTJXM.*.*(..))"/>--> <aop:pointcut id="One" expression="execution(* *..ASPECTJXM.*.One(..))"/> <aop:pointcut id="Serend" expression="execution(* *..ASPECTJXM.*.Serend(..))"/> <aop:pointcut id="Thire" expression="execution(* *..ASPECTJXM.*.Thire(..))"/> <!--ref增强对象--> <aop:aspect ref="myAspectj"> <!--method增强方法--> <aop:before method="before" pointcut-ref="One"></aop:before> <aop:before method="before(org.aspectj.lang.JoinPoint)" pointcut-ref="One"></aop:before> <aop:after-returning method="afterReturning" pointcut-ref="Serend"></aop:after-returning> <aop:after-returning method="afterReturning(java.lang.String)" pointcut-ref="Serend" returning="result"></aop:after-returning> <aop:around method="around" pointcut-ref="Serend"/> <aop:after-throwing method="afterThrowing" pointcut-ref="Thire"/> <aop:after-throwing method="afterThrowing(java.lang.Exception)" pointcut-ref="Thire" throwing="ex"/> <aop:after method="after" pointcut-ref="Thire"></aop:after> </aop:aspect> </aop:config> </beans>



//创建一个接口
public interface ISome {
    public void One();
    public String Serend();
    public void Thire();

}
//创建一个接口的实现类
public class SomeSevice implements ISome {
    public void One() {
        System.out.println("One==================");
    }

   public String Serend() {
       System.out.println("Send==============");
       return "返回abc";
   }

    public void Thire() {
       System.out.println("Third============");
        int i=5/0;//制造假异常
    }

}
//切面
public class MyAspectj {

//前置通知

public void before(){ System.out.println("前置通知方法before()"); } public void before(JoinPoint jp){ System.out.println("前置通知方法before() jp="+jp); }

//后置通知

public void afterReturning(){ System.out.println("后置通知方法"); } public void afterReturning(String result){ System.out.println("后置通知方法 result="+result); }

//环绕通知

public Object around(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("环绕通知方法,目标方法执行之前"); //执行目标方法 Object result = pjp.proceed(); //System.out.println(result); System.out.println("环绕通知方法,目标方法执行之后"+pjp); return ((String)result).toUpperCase(); }

//异常通知

public void afterThrowing(){ System.out.println("异常通知方法"); } public void afterThrowing(Exception ex){ System.out.println("异常通知方法 ex="+ex.getMessage()); }

//最终通知

public void after(){ System.out.println("最终通知方法"); } } //单测 @org.junit.Test //ASPECTJXML public void ASPECTJXML() { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextASPECTXML.xml"); cn.happy.ASPECTJXM.ISome servic= (cn.happy.ASPECTJXM.ISome)ctx.getBean("someService"); servic.One(); servic.Serend(); servic.Thire(); } //输出结果 前置通知方法before() 前置通知方法before() jp=execution(void cn.happy.ASPECTJXM.ISome.One()) One================== 环绕通知方法,目标方法执行之前 Send============== 环绕通知方法,目标方法执行之后execution(String cn.happy.ASPECTJXM.ISome.Serend()) 后置通知方法 result=返回ABC 后置通知方法 Third============ 最终通知方法 异常通知方法 ex=/ by zero 异常通知方法 java.lang.ArithmeticException: / by zero

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值