Java spring基于XML的aop配置实现

2 篇文章 0 订阅

1.依赖包

2.文件结构

3.接口类ISomeService

package com.buckwheats.test;

public interface ISomeService {
    public void doFirst(String user);
    public String doSecond(String user);
    public void doThird();
}

4.ISomeService的实现类SomeService

package com.buckwheats.test;

public class SomeService implements ISomeService {
    @Override
    public void doFirst(String user) {
        System.out.println("执行doFirst");
    }

    @Override
    public String doSecond(String user) {
        System.out.println("执行doSecond");
        return "buckwheat";
    }

    @Override
    public void doThird() {
        System.out.println("执行doThird" +3/0);
    }
}

5.切入类MySystem

package com.buckwheats.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import java.util.Arrays;
public class MySystem {
    //前置方法,指定doFirst为切入点
    public void befor(){
        System.out.println("执行前置方法");
    }
    public void befor(JoinPoint jp){
        System.out.println("执行前置方法 参数user:"+Arrays.toString(jp.getArgs()));
    }
    //后置方法,无参数
    public void AfterReturning(){
        System.out.println("执行后置方法 不带参数");
    }
    //后置方法,有参数
    public void AfterReturning(JoinPoint jp){
        //输出传过来的参数
        //直接输出jp.getArgs()会得到一个数组对象,但是并不知道里面的内容,需要调用Arrays的toString()方法
        System.out.println("执行后置方法 带参数 args:"+ Arrays.toString(jp.getArgs()));
    }
    //环绕通知,可以改变目标方法的返回参数
    public String myAround(ProceedingJoinPoint result) throws Throwable {
        System.out.println("执行环绕通知,前置方法");
        Object obj = result.proceed();
        //获取传过来的参数
        //获取直接输出jp.getArgs()会得到一个数组对象,但是并不知道里面的内容,需要调用Arrays的toString()方法
        System.out.println("执行环绕通知 获取参数 args:"+ Arrays.toString(result.getArgs()));
        System.out.println("执行环绕通知,后置方法");
        return ((String)obj).toUpperCase();
    }
    //异常通知,当doThird出现异常时执行
    public void myAfterThrowing(Exception ex){
        System.out.println("出现异常 e:"+ex);
    }
    //"最终通知,不管是否出现异常,这个方法都会执行,且在最后执行
    public void myAfter(){
        System.out.println("最后通知,不管是否出现异常,这个方法都会执行");
    }
    //最终通知,有参数
    public void myAfter(JoinPoint jp){
        System.out.println("执行最终方法 ,带参数,切入点(方法):"+jp.getSignature().getName());
    }
}

6.配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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="com.buckwheats.test.SomeService"/>
    <!--注册切面-->
    <bean id="myAdvice" class="com.buckwheats.test.MySystem"/>
    <aop:config >
        <aop:aspect ref="myAdvice">
            <aop:pointcut id="doFirst" expression="execution(* *..ISomeService.doFirst(..))"/>
            <aop:pointcut id="doSecond" expression="execution(* *..ISomeService.doSecond(..))"/>
            <aop:pointcut id="doThird" expression="execution(* *..ISomeService.doThird(..))"/>
            <aop:before method="befor" pointcut-ref="doFirst"/><!--前置方法,不带参数-->
            <aop:before method="befor(org.aspectj.lang.JoinPoint)"  pointcut-ref="doFirst"/><!--前置方法,带参数-->
            <aop:after-returning method="AfterReturning" pointcut-ref="doSecond" /> <!--后置方法,不带参数-->
            <aop:after-returning method="AfterReturning(org.aspectj.lang.JoinPoint)" pointcut-ref="doSecond" />
            <aop:around method="myAround" pointcut-ref="doSecond" /> <!--环绕通知-->
            <aop:after-throwing method="myAfterThrowing" pointcut-ref="doThird" throwing="ex"/> <!--执行异常通知-->
            <aop:after method="myAfter" pointcut-ref="doThird" /><!--执行最终方法,不管是否出现在异常都会执行-->
            <aop:after method="myAfter(org.aspectj.lang.JoinPoint)" pointcut-ref="doFirst" />
        </aop:aspect>
    </aop:config>
</beans>

7.测试类Test

package com.buckwheats.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    @org.junit.Test
    public void test01(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("com/buckwheats/test/applicationContext.xml");
        ISomeService service = (ISomeService) ac.getBean("someService");
        service.doFirst("oooo");
        System.out.println("=========");
        System.out.println(service.doSecond("iii"));
        System.out.println("==============");
        service.doThird();
    }
}

8.输出结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值