以XML方式配置AOP切面

概述

除了使用AspectJ注解声明切面,Spring也支持在bean配置文件中声明切面。这种声明是通过aop名称空间中的XML元素完成的。
正常情况下,基于注解的声明要优先于基于XML的声明。通过AspectJ注解,切面可以与AspectJ兼容,而基于XML的配置则是Spring专有的。由于AspectJ得到越来越多的 AOP框架支持,所以以注解风格编写的切面将会有更多重用的机会。

配置细节

在bean配置文件中,所有的Spring AOP配置都必须定义在aop:config元素内部。对于每个切面而言,都要创建一个aop:aspect元素来为具体的切面实现引用后端bean实例。
切面bean必须有一个标识符,供aop:aspect元素引用。
在这里插入图片描述

声明切入点

  • 1)切入点使用aop:pointcut元素声明。
  • 2)切入点必须定义在aop:aspect元素下,或者直接定义在aop:config元素下。
    ① 定义在aop:aspect元素下:只对当前切面有效
    ② 定义在aop:config元素下:对所有切面都有效
  • 3)基于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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="com.atguigu.spring.day03_Spring.aopxml"/>
    <aop:config>
        <aop:aspect ref="myLogger1">
            <aop:pointcut id="cut" expression="execution(* com.atguigu.spring.day03_Spring.aopxml.*.*(..))"/>
            <!--<aop:before method="before" pointcut="execution(* com.atguigu.spring.day03_Spring.aopxml.*.*(..))"/>-->
            <aop:before method="before" pointcut-ref="cut"/>
        </aop:aspect>

    </aop:config>
</beans>

声明通知

  • 1)在aop名称空间中,每种通知类型都对应一个特定的XML元素。
  • 2)通知元素需要使用<pointcut-ref来引用切入点,或用<pointcut直接嵌入切入点表达式。
  • 3)method属性指定切面类中通知方法的名称
    在这里插入图片描述
    代码:
    写一个目标类:
public interface Mathl {
    int add(int i, int j);
    int sub(int i, int j);
    int mul(int i, int j);
    int div(int i, int j);
}

@Component
public class MathImpl implements Mathl {
    @Override
    public int add(int i, int j) {
        return i + j;
    }

    @Override
    public int sub(int i, int j) {
        return i-j;
    }

    @Override
    public int mul(int i, int j) {
        return i*j;
    }

    @Override
    public int div(int i, int j) {
        return i/j;
    }
}

编写一个切面:

@Component
public class MyLogger1 {

    public void before(){
        System.out.println("前置通知!");
    }
}

在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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="com.atguigu.spring.day03_Spring.aopxml"/>
    <aop:config>
        <aop:aspect ref="myLogger1">
            <aop:pointcut id="cut" expression="execution(* com.atguigu.spring.day03_Spring.aopxml.*.*(..))"/>
            <!--<aop:before method="before" pointcut="execution(* com.atguigu.spring.day03_Spring.aopxml.*.*(..))"/>-->
            <aop:before method="before" pointcut-ref="cut"/>
        </aop:aspect>

    </aop:config>
</beans>

测试:

public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("aop-xml.xml");
        Mathl bean = ac.getBean("mathImpl", Mathl.class);
        int result = bean.add(2, 3);
        System.out.println(result);
    }
}

输出结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值