20200114——spring基于xml的aop配置

配置bean.xml 中aop配置文件

<?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">
</beans>

创一个accountService 使bean去管理
在创建一个logger 让bean去管理。
我们的目的是在每一个accountService的方法被调用的时候,都能执行这个logger方法
再配置一个aop


    <aop:config>
        <aop:aspect id="logAdvice" ref="logger">
            <aop:before method="printLog"></aop:before>
        </aop:aspect>
    </aop:config>

到此,还没有结束,因为logger与accountService根本没有联系
使用pointcut = execution(表达式) 这样就可以有关系了。

<aop:before method="printLog" pointcut="execution(public void com.itheima.service.impl.AccoutServiceImpl.saveAccout())"></aop:before>

测试类

public class AOPTest {
    public static void main(String[] args) {
        ApplicationContext  applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        IAccoutService iAccoutService = (IAccoutService)applicationContext.getBean("accoutService");
        iAccoutService.saveAccout();
    }
}

第一步:引入spring容器
第二步:从容器中获取accountService对象
第三步:执行方法

在这里插入图片描述
配置ok了

关于表达式写法
访问修饰符可以省略
返回值可以使用通配符,表示任意返回值
包名可以使用通配符 表示任意包
有几级包,就需要写几个*
… 包含包以及以下所有包
类名和方法名都可以使用通配

实际开发中切入点表达式的通常写法:
切到业务层实现类下的所有方法

<!-- 配置前置通知:在切入点方法执行之前执行
            <aop:before method="beforePrintLog" pointcut-ref="pt1" ></aop:before>-->

            <!-- 配置后置通知:在切入点方法正常执行之后值。它和异常通知永远只能执行一个
            <aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>-->

            <!-- 配置异常通知:在切入点方法执行产生异常之后执行。它和后置通知永远只能执行一个
            <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>-->

            <!-- 配置最终通知:无论切入点方法是否正常执行它都会在其后面执行
            <aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>-->

            <!-- 配置环绕通知 详细的注释请看Logger类中-->
            <aop:around method="aroundPringLog" pointcut-ref="pt1"></aop:around>

声明一个pt1,可以节省

当我们配置了环绕通知之后,切入点方法没有执行,通知方法执行了。
分析:通过对比环绕通知的动态代理方法,发现动态代理中的环绕通知有明确的业务层方法调用,而我们的代码中没有
解决:提供了一个接口,proceedingJoinPoint 该接口有一个方法,此方法相当于切入点方法,该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类

 public Object aroundPringLog(ProceedingJoinPoint pjp){
        Object rtValue = null;
        try{
            Object[] args = pjp.getArgs();//得到方法执行所需的参数

            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。前置");

            rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)

            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。后置");

            return rtValue;
        }catch (Throwable t){
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。异常");
            throw new RuntimeException(t);
        }finally {
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。最终");
        }
    }
发布了657 篇原创文章 · 获赞 39 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_36344771/article/details/103971074
今日推荐