AOP的XML方式的实现

和之前关于Aop的注解方式的实现一样,首先需要配置pom.xml。配置aop的环境。其次计算器Calculator类与实现类CalculatorImpl没有改变。有变化的是切面类即CalculatorLogging。所有的方法不要注解,如下:

package com.yc.spring.aop.impl;

import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

@Component("calculatorlogging")
public class CalculatorLogging {


    public void beforeMethod(JoinPoint jp){
        String method = jp.getSignature().getName();
        Object[] objs = jp.getArgs();
        System.out.println("开始执行"+method+ ",计算数据是:" + Arrays.toString(objs));
    }

    public void afterReturingMethod(JoinPoint jp, Object result){
        String method = jp.getSignature().getName();
        System.out.println("执行"+method + "获得的结果是" + result);
    }

    public void afterThrowingMethod(JoinPoint jp,Throwable e){
        String method = jp.getSignature().getName();
        System.out.println("执行"+method + "发生"+ e.getMessage());
    }

    public void afterMethod(JoinPoint jp){
        String method = jp.getSignature().getName();
        System.out.println("执行"+method + "完成...");
    }

    //这里补充了上篇没有讲到的around
    public Object around(ProceedingJoinPoint pjp){
        String method = pjp.getSignature().getName();
        Object[] objs = pjp.getArgs();
        System.out.println("开始执行"+method+ ",计算数据是:" + Arrays.toString(objs));
        Object retVal=null;
        try {
            //调用核心逻辑  
            retVal = pjp.proceed(); 
            System.out.println("执行"+method + "完成...");
        } catch (Throwable e) {
            System.out.println("执行"+method + "发生"+ e.getMessage());
        }
        System.out.println("执行"+method + "获得的结果是" + retVal);
        return retVal;
    }

}

我们在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:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- bean就由spring容器创建好的对象 -->

    <!-- 指定可以做为spring容器管理的对象的包 -->
    <context:component-scan base-package="com.yc.spring" />

    <bean id="calculatorLogging" class="com.yc.spring.aop.impl.CalculatorLogging"></bean>

    <aop:config>
        <aop:pointcut expression="execution(public * com.yc.spring.aop..*.*(..))" id="myPointcut" /> <!--  配置切点 -->
        <aop:aspect id="myAspect" ref="calculatorLogging">
            <aop:before method="beforeMethod" pointcut-ref="myPointcut"/>
            <aop:after method="afterMethod" pointcut-ref="myPointcut" />
            <aop:after-returning method="afterReturingMethod" pointcut-ref="myPointcut" returning="result"/>
            <aop:after-throwing method="afterThrowingMethod" pointcut-ref="myPointcut"  throwing="e" />  
            <!--<aop:around method="around" pointcut-ref="myPointcut" />-->
        </aop:aspect>
    </aop:config>

</beans>

aop:pointcut                     声明切入点
aop:aspect                        声明切面
aop:before                        声明前置通知
aop:after                           声明后置通知
aop:after-returning           声明后置返回通知
aop:after-throwing           声明后置异常通知
aop:around                       声明环绕通知

切入点:其定义在config的第一层子目录,可以被其他所有的aspect所引用;当然pointcut还可以定义在aop:aspect的子目录,但是此时只能被该aspect所引用;id定义的是该pointcut的名字;aop:pointcut可以单独定义,也可以使用 aop:around中所示的定义方法
切面:id是指该切面的名字,ref指的是它所引用的切面类
通知:method指的是其所位于的aspect所引用的切面类中的方法名,pointcut-ref指的是引用的切点

后置返回通知需要配置returning,且规则与注解方式一致,需要保持与后置返回通知方法的参数名一致。
后置异常通知,需要配置args-names和throwing,且保持与方法中异常的参数名一致

<think>好的,我现在需要帮助用户了解如何使用XML方式Spring框架中实现AOP。首先,我需要回忆Spring AOP的基本概念和XML配置的步骤。 用户提供的引用内容提到了通过<aop:config>和<aop:aspect>来配置切面,并且需要定义切入点和通知。我应该分步骤解释这个过程,确保结构清晰。 首先,创建项目结构是基础,需要引入Spring的依赖,比如在Maven中添加spring-aop和aspectjweaver。然后,定义目标接口和实现类,比如UserService和UserServiceImpl。接下来,创建切面类,包含通知方法,比如日志记录的方法。 然后,配置XML文件是关键部分。需要声明目标对象和切面类的Bean,然后在<aop:config>中配置切面,引用切面Bean,并定义切入点和通知。切入点使用<aop:pointcut>,通知类型如前置通知<aop:before>,后置通知<aop:after-returning>等。 需要确保用户理解每个配置元素的作用,比如aspect的ref属性指向切面Bean,order属性指定优先级。同时,切入点表达式如何指定目标方法,例如execution(* com.example.service.UserService.*(..))。 还要注意,XML配置和注解可以混用,但这里专注于XML方式。可能还需要提醒用户关于AOP的依赖,以及如何测试配置是否正确,比如通过ApplicationContext获取Bean并调用方法。 最后,生成相关问题,可能涉及配置步骤、切入点表达式、通知类型比较,以及混合配置的可能性。确保问题覆盖用户可能感兴趣的扩展内容。 检查是否有遗漏的步骤,比如没有提到异常通知或环绕通知,需要在示例中包含吗?根据引用中的例子,用户可能只需要基本的前置和后置,但最好提到其他类型。此外,确保LaTeX格式正确,变量用$包裹,如UserService,但根据规则,中文回答中可能不需要变量格式,只需数学表达式。需要确认用户示例中的变量是否用$,但示例中的快速排序代码并没有变量使用LaTeX,所以可能用户的问题中提到的数学表达式才需要,其他如类名不需要。所以可能不需要在类名或方法名中使用$,只需在数学公式时使用。 总结步骤:项目结构、目标类、切面类、XML配置(声明Bean、配置AOP、定义切入点和通知)、测试步骤。然后相关问题可能涉及具体配置细节、表达式写法、优缺点比较等。</think>### 使用XML配置实现Spring AOP的步骤 #### 1. **项目准备** - 在Maven项目中引入Spring AOP相关依赖: ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> ``` - 创建目标接口和实现类,例如: ```java public interface UserService { void addUser(String name); } public class UserServiceImpl implements UserService { public void addUser(String name) { System.out.println("添加用户:" + name); } } ``` #### 2. **定义切面类** 切面类包含通知(Advice)方法,例如日志记录: ```java public class LoggingAspect { public void beforeAdvice() { System.out.println("方法执行前:记录日志"); } public void afterAdvice() { System.out.println("方法执行后:记录结果"); } } ``` #### 3. **配置XML文件** 在Spring配置文件中声明目标对象和切面Bean,并配置AOP: ```xml <!-- 声明目标对象 --> <bean id="userService" class="com.example.service.UserServiceImpl"/> <!-- 声明切面类 --> <bean id="loggingAspect" class="com.example.aspect.LoggingAspect"/> <!-- 配置AOP --> <aop:config> <!-- 定义切入点:匹配UserService的所有方法 --> <aop:pointcut id="serviceMethods" expression="execution(* com.example.service.UserService.*(..))"/> <!-- 配置切面,引用loggingAspect并设置优先级 --> <aop:aspect id="logAspect" ref="loggingAspect" order="1"> <!-- 前置通知 --> <aop:before method="beforeAdvice" pointcut-ref="serviceMethods"/> <!-- 后置通知 --> <aop:after-returning method="afterAdvice" pointcut-ref="serviceMethods"/> </aop:aspect> </aop:config> ``` #### 4. **测试AOP配置** 加载Spring上下文并调用目标方法: ```java public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = context.getBean("userService", UserService.class); userService.addUser("张三"); } } ``` 输出结果: ``` 方法执行前:记录日志 添加用户:张三 方法执行后:记录结果 ``` ### 关键配置说明 1. **切入点表达式** 使用`execution(* com.example.service.UserService.*(..))`匹配`UserService`接口的所有方法。 表达式语法:`execution(返回类型 包名.类名.方法名(参数))`[^2]。 2. **通知类型** - `<aop:before>`:方法执行前触发。 - `<aop:after-returning>`:方法正常返回后触发。 - `<aop:after-throwing>`:方法抛出异常时触发。 - `<aop:around>`:包裹目标方法,可控制执行流程。 3. **切面优先级** 通过`order`属性指定,值越小优先级越高。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值