Spring_AOP_XML

来源:互联网 发布:淘宝上牛仔裤 编辑:程序博客网 时间:2024/06/05 16:12

本次代码基于代码笔记中的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方法。    -->
0 0
原创粉丝点击