spring基于xml配置AOP,的4种常用 切入点表达式的 步骤写法

1.导入aspectjweaver是spring的切入点表达式需要用的包
在这里插入图片描述

  <dependency>
        <!--spring 包-->
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>

    <dependency>
        <!--aspectjweaver是spring的切入点表达式需要用的包-->
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.7</version>
    </dependency>

2.创建业务逻辑层接口类和实现类,的增,删,改方法
在这里插入图片描述3.写通知类中的具体方法 :Logger
在这里插入图片描述

//记录日志的工具类(也叫通知类),它里面提供了公共的代码
public class Logger {
    public void beforePrintlog(){
        //用于打印日志:计划让其在切入点方法执行前执行,也就是前置通知
        //切入点方法就是业务层的方法
        System.out.println("logger类中 前置通知 方法开始记录日志了。。。");
    }
    public void afterReturningPrintlog(){
        //后置通知
        System.out.println("logger类中 后置通知 方法开始记录日志了。。。");
    }
    public void afterThrowingPrintlog(){
        //异常通知
        System.out.println("logger类中 异常通知 方法开始记录日志了。。。");
    }
    public void afterPrintlog(){
        //最终通知
        System.out.println("logger类中 最终通知 方法开始记录日志了。。。");
    }
}

4.配置通知,IOC,AOC,bean.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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--自己理解后的简单解释:
    1.accountService 类(账户)想增强,他会的技能,并且要使用技能的时候,
        都能有人监督并给出通知和建议(生成通知)。
    2.就请了个可以 监督的通知类,它有通知的技能
    3.通知就告诉账户,在监督账户使用技能的时候,
        通知会在,账户使用具体某个技能的时候准备,  前置,后置,异常,最终
        的一些相关的摄像机在进行监督。
    -->

    <!--配置spring的IOC ,把service 对象配置进来-->
    <bean id = "accountService" class="wuwei.service.impl.AccountService"></bean>
    <!--spring中 基于xml ,AOP配置步骤
        1.把通知Bean也交给spring来管理
        2.使用aop:config标签表明开始AOP配置
        3.使用aop:aspect标签表明配置切面
            id属性:是给切面提供一个唯一标识
            ref属性:是指通知类bean的id
         4.在aop:aspect标签的内部使用对应的标签来配置通知的类型
            我们现在示例是让pringLog方法在切入点方法执行前,所以是前置通知。
            aop:before:表示配置前置通知
                method属性:用于指定Logger类中哪个方法是前置通知
                pointcut属性:用于指定切入点表达式,该表达式的含义是指对业务层
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值