Spring基础篇:使用XML方式实现Spring提供的AOP

AOP:核心是在不改动源代码时,对已有代码进行增强。这个增强操作可以是业务逻辑外的其他重复出现的操作或任务。

前期准备

以Spring为框架,只需要准备一个待加强的类。

以一个用户登录的例子,这个类中有两个方法,登录和登出。现在需要做一件事,在登录前输出当前时刻,在登出前也输出当前时刻。

public class UserService {
    public void login(){
        // 增强1
        System.out.println("用户登录");
    }

    public void logOut(){
        // 增强2
        System.out.println("用户登出");
    }
}

增强类

这里使用了日期模板,以及使用模板输出时间。

public class TimePrint {
    SimpleDateFormat matter = new SimpleDateFormat("现在时间:yyyy年MM月dd日E HH时mm分ss秒");
    public void printTime(){
        System.out.println(matter.format(new Date()));
    }
}

注意事项:

Spring底层还是使用的是aspectj的方式,所以一定要再加上该依赖。

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.1</version>
</dependency>

使用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
                           https://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

将类注册到容器中

待增强类 和 增强的工具类都注入到容器中

<bean id="userService" class="juejin.aopXml.bean.UserService"></bean>
<bean id="timePrint" class="juejin.aopXml.utils.TimePrint"></bean>

使用配置的方式,配置aop

固定写法:

其中,<aop:aspect id="timeAspect" ref="timePrint">,重要的是ref指向的是增强类。

<aop:config>
    <aop:aspect id="timeAspect" ref="timePrint">
        
    </aop:aspect>
</aop:config>

Ok,Ok。现在暂停了解一下Spring给我们提供的加强类型有哪些(简单点说就是,你希望加强的方法执行具体方法的什么时候去执行)。主要有以下几个(该无序内容引自:从 0 开始深入学习 Spring - LinkedBear - 掘金小册 (juejin.cn)):

  • before 前置通知:目标对象的方法调用之前触发。
  • after后置通知:目标对象的方法调用之后触发。
  • afterReturning 返回通知:目标对象的方法调用完成,在返回结果值之后触发。
  • afterThrowing 异常通知:目标对象的方法运行中抛出 / 触发异常后触发。
  • around 环绕通知:编程式控制目标对象的方法调用。

那么目标很明确,我们要做的是目标对象的方法调用之前触发,也就是在执行System.out.println("用户登录");之前,执行System.out.println(new Date().getTime());

了解了加强类型,我们需要写的是,告诉Spring哪个方法需要被增强。先看我直接写出来,再理解其中的意思。

<aop:before method="printTime" pointcut="execution(public * juejin.aopXml.bean.UserService.* (..))"></aop:before>
  1. 内容都被<aop:before></aop:before>包围着,这就是上面说的:前置通知,目标对象的方法调用之前触发。
  2. method="printTime":表示调用增强类中的printTime()方法。
  3. pointcut="" :表示上面的具体要给哪个类中的哪个方法进行printTime()增强。
  4. 这的切点表达式意思是: 方法要求是:public的,范围值任意,在juejin.aopXml.bean包下,在UserService类中的任意方法,参数个数任意。

pointcut="",这个引号中的内容是关于切点表达式的内容,这个具体的使用内容因篇幅原因在其他文章中介绍。

启动类

使用了线程的睡眠方式,睡眠一秒,测试两个方法是否被增强。

public class AopXmlApplication {
    public static void main(String[] args) throws InterruptedException {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("juejin/aopXml/xmlAop.xml");
        UserService userService = ctx.getBean(UserService.class);
        userService.login();
        Thread.sleep(1000);
        userService.logOut();
    }
}

输出:

现在时间:2022年12月04日星期日 21时23分51秒
用户登录
现在时间:2022年12月04日星期日 21时23分52秒
用户登出

附录:完整的XML-aop配置内容

    <aop:config>
        <aop:aspect id="timeAspect" ref="timePrint">
            <aop:before method="printTime" pointcut="execution(public * juejin.aopXml.bean.UserService.* (..))"></aop:before>
        </aop:aspect>
    </aop:config>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值