Spring_AOP_XML

本次代码基于代码笔记中的Spring_AOP_Annotation修改,命名为Spring _AOP_XML


将LogInterceptor.java中的注释销掉,现在就成为了只有切面,但切面没有运行的状态。现在让我们来配置beans.xml,用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:annotation-config></context:annotation-config>
        <context:component-scan base-package="main.com"></context:component-scan>
       <!-- <bean id="logInterceptor" class="main.com.ly.aop.LogInterceptor"></bean>-->

        <aop:config>
            <aop:pointcut id="servicePointcut" expression="execution(public * main.com.ly.service..*.add(..))"></aop:pointcut>
            <!--此时的pointcut是全局的pointcut,而在aspect中也可以添加pointcut,二者的区别当然是使用范围不同啦。-->
            <aop:aspect id="logAspect" ref="logInterceptor">
                <aop:before method="before" pointcut-ref="servicePointcut"></aop:before>
            </aop:aspect>
        </aop:config>
    </beans>

    <!--
    pointcut:pointcut。也就是说明在哪些方法上加上切面逻辑。
    aspect:找到切面的那个对象
    ref:创建的切面类
    logInterceptor:切面对象

    总结来说:这个配置解决了两个问题:在哪加切面和加什么的问题
    pointcut说明的是在哪加切面的问题
    aspect说的是这是一个切面,
    然后将logIntercepter对象当作切面注入进来,
    现在就有了一个真正的切面对象了。
    before这句说的是在执行add方法之前先执行切面对象的before方法。
    -->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值