跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
最近更改
随机页面
MediaWiki帮助
代码酷
搜索
搜索
中文(中国大陆)
外观
创建账号
登录
个人工具
创建账号
登录
未登录编辑者的页面
了解详情
贡献
讨论
编辑“︁
Spring XML配置AOP
”︁(章节)
页面
讨论
大陆简体
阅读
编辑
编辑源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
编辑
编辑源代码
查看历史
常规
链入页面
相关更改
特殊页面
页面信息
外观
移至侧栏
隐藏
您的更改会在有权核准的用户核准后向读者展示。
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
= Spring XML配置AOP = '''Spring XML配置AOP'''是Spring框架中通过XML文件定义面向切面编程(AOP)的方式。它允许开发者在不修改原有业务逻辑代码的情况下,通过配置的方式实现横切关注点(如日志、事务、权限等)的模块化。 == 介绍 == AOP(Aspect-Oriented Programming)是一种编程范式,用于将与核心业务逻辑无关的横切关注点(如日志记录、事务管理等)从业务代码中分离出来,从而提高代码的模块化和可维护性。Spring AOP支持两种配置方式:基于注解和基于XML。本文重点介绍'''XML配置方式'''。 在XML配置中,开发者通过定义'''切面(Aspect)'''、'''通知(Advice)'''、'''切入点(Pointcut)'''等元素,将横切逻辑织入目标方法。 == 核心概念 == 以下是Spring AOP的核心概念及其在XML中的对应配置: === 1. 切面(Aspect) === 切面是横切关注点的模块化表示。在XML中,通过`<aop:aspect>`标签定义。 === 2. 通知(Advice) === 通知是切面在特定连接点(如方法调用)执行的动作。Spring支持以下通知类型: * '''前置通知(Before)''':在目标方法执行前执行。 * '''后置通知(After)''':在目标方法执行后执行(无论是否抛出异常)。 * '''返回通知(After-returning)''':在目标方法成功返回后执行。 * '''异常通知(After-throwing)''':在目标方法抛出异常后执行。 * '''环绕通知(Around)''':在目标方法执行前后都执行,可控制是否执行目标方法。 === 3. 切入点(Pointcut) === 切入点是一个表达式,用于匹配目标方法。Spring使用AspectJ的切入点表达式语法。 == XML配置示例 == 以下是一个完整的XML配置AOP示例,展示如何为一个简单的服务类添加日志功能。 === 目标服务类 === <syntaxhighlight lang="java"> public class UserService { public void addUser(String username) { System.out.println("添加用户: " + username); } public void deleteUser(String username) { System.out.println("删除用户: " + username); } } </syntaxhighlight> === 日志切面类 === <syntaxhighlight lang="java"> public class LoggingAspect { public void beforeAdvice(JoinPoint joinPoint) { System.out.println("准备执行: " + joinPoint.getSignature().getName()); } public void afterAdvice(JoinPoint joinPoint) { System.out.println("执行完成: " + joinPoint.getSignature().getName()); } } </syntaxhighlight> === XML配置文件 === <syntaxhighlight lang="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: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 http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 定义目标Bean --> <bean id="userService" class="com.example.UserService"/> <!-- 定义切面Bean --> <bean id="loggingAspect" class="com.example.LoggingAspect"/> <!-- AOP配置 --> <aop:config> <aop:aspect id="logAspect" ref="loggingAspect"> <!-- 定义切入点 --> <aop:pointcut id="serviceMethods" expression="execution(* com.example.UserService.*(..))"/> <!-- 前置通知 --> <aop:before method="beforeAdvice" pointcut-ref="serviceMethods"/> <!-- 后置通知 --> <aop:after method="afterAdvice" pointcut-ref="serviceMethods"/> </aop:aspect> </aop:config> </beans> </syntaxhighlight> === 测试代码 === <syntaxhighlight lang="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("张三"); userService.deleteUser("李四"); } } </syntaxhighlight> === 输出结果 === <pre> 准备执行: addUser 添加用户: 张三 执行完成: addUser 准备执行: deleteUser 删除用户: 李四 执行完成: deleteUser </pre> == 切入点表达式详解 == Spring AOP使用AspectJ的切入点表达式语法。以下是一些常见用法: * `execution(* com.example.service.*.*(..))`:匹配`com.example.service`包下所有类的所有方法。 * `execution(* com.example.service.UserService.add*(..))`:匹配`UserService`类中以`add`开头的所有方法。 * `within(com.example.service.*)`:匹配`com.example.service`包下的所有类。 * `@annotation(com.example.Loggable)`:匹配带有`@Loggable`注解的方法。 == 实际应用场景 == === 场景1:事务管理 === 通过AOP可以实现声明式事务管理。以下是一个简化的XML配置示例: <syntaxhighlight lang="xml"> <aop:config> <aop:pointcut id="txMethods" expression="execution(* com.example.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txMethods"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> </syntaxhighlight> === 场景2:性能监控 === 可以创建一个监控切面,记录方法执行时间: <syntaxhighlight lang="java"> public class PerformanceAspect { public Object monitorPerformance(ProceedingJoinPoint pjp) throws Throwable { long start = System.currentTimeMillis(); Object result = pjp.proceed(); long duration = System.currentTimeMillis() - start; System.out.println(pjp.getSignature() + " 执行时间: " + duration + "ms"); return result; } } </syntaxhighlight> XML配置: <syntaxhighlight lang="xml"> <aop:config> <aop:aspect ref="performanceAspect"> <aop:around method="monitorPerformance" pointcut="execution(* com.example.service.*.*(..))"/> </aop:aspect> </aop:config> </syntaxhighlight> == 高级主题 == === 引入(Introduction) === 引入允许向现有类添加新的接口实现。例如,我们可以让一个服务类实现`Monitorable`接口: <syntaxhighlight lang="xml"> <aop:config> <aop:aspect> <aop:declare-parents types-matching="com.example.service.UserService" implement-interface="com.example.Monitorable" default-impl="com.example.MonitorableImpl"/> </aop:aspect> </aop:config> </syntaxhighlight> === 通知参数 === 可以通过`arg-names`属性将方法参数传递给通知: <syntaxhighlight lang="java"> public void logParams(JoinPoint jp, String username) { System.out.println("参数值: " + username); } </syntaxhighlight> XML配置: <syntaxhighlight lang="xml"> <aop:before method="logParams" pointcut="execution(* com.example.UserService.*(String)) and args(username)" arg-names="username"/> </syntaxhighlight> == 总结 == Spring XML配置AOP提供了一种声明式的方式来管理横切关注点,具有以下优点: * 业务逻辑与横切关注点分离 * 配置集中管理,易于维护 * 支持丰富的切入点表达式 * 可以与Spring其他功能(如事务管理)无缝集成 对于复杂的AOP需求,建议结合使用XML和注解配置,以获得更好的灵活性和可读性。 == 参见 == * [[Spring AOP基础]] * [[Spring注解配置AOP]] * [[AspectJ切入点表达式]] [[Category:后端框架]] [[Category:Spring]] [[Category:Spring AOP]]
摘要:
请注意,所有对代码酷的贡献均被视为依照知识共享署名-非商业性使用-相同方式共享发表(详情请见
代码酷:著作权
)。如果您不希望您的文字作品被随意编辑和分发传播,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)