除了使用AspectJ注解声明切面,Spring 也支持在 Bean 配置文件中声明切面。这种声明是通过 aop schema 中的 XML 元素完成的。正常情况下,基于注解的声明要优先于基于 XML 的声明。通过 AspectJ 注解,切面可以与 AspectJ 兼容,而基于 XML 的配置则是 Spring 专有的。由于 AspectJ 得到越来越多的 AOP 框架支持,所以以注解风格编写的切面将会有更多重用的机会。
使用SpringAOP实现
1. AOP依赖包:
1.1 spring包:
核心包:
spring-framework-4.0.0.RELEASE-dist:
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
适配AspectJ包:
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
1.2 commons包:
commons-logging-1.1.1.jar
1.3 aspectj包:
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
2. SpringAOP配置步骤:
2.1 加入AOP依赖包;
2.2 在配置文件中加入aop的命名空间;
2.3 基于XML的方式;
2.3.1 声明切面
当使用 XML 声明切面时,需要在 <beans> 根元素中导入 aop Schema。在 Bean 配置文件中,所有的 Spring AOP 配置都必须定义在 <aop:config> 元素内部。对于每个切面而言,都要创建一个 <aop:aspect> 元素来为具体的切面实现引用后端 Bean 实例。切面 Bean 必须有一个标示符,供 <aop:aspect> 元素引用。
2.3.2 声明切入点
切入点使用 <aop:pointcut> 元素声明。切入点必须定义在 <aop:aspect> 元素下,或者直接定义在 <aop:config> 元素下。定义在 <aop:aspect> 元素下: 只对当前切面有效。定义在 <aop:config> 元素下::对所有切面都有效。基于 XML 的 AOP 配置不允许在切入点表达式中用名称引用其他切入点。
2.3.3 声明通知
在 aop Schema 中,每种通知类型都对应一个特定的 XML 元素。通知元素需要使用 <pointcut-ref> 来引用切入点, 或用 <pointcut> 直接嵌入切入点表达式。method 属性指定切面类中通知方法的名称。
2.3.4 声明引入
可以利用 <aop:declare-parents> 元素在切面内部声明引入。
3. 添加切面模型类并配置
package xyz.huning.spring4.aop.calculator.aopimpl.xml; import java.util.Arrays; import org.aspectj.lang.JoinPoint; public class CalculatorValidateAspect { public void validate(JoinPoint joinPoint) { System.out.println("validate parameter: " + Arrays.toString(joinPoint.getArgs())); } }
package xyz.huning.spring4.aop.calculator.aopimpl.xml; import org.aspectj.lang.JoinPoint; public class CalculatorLogAspect { public void before(JoinPoint joinPoint) { System.out.println("before: " + "[x=" + joinPoint.getArgs()[0] + ", y=" + joinPoint.getArgs()[1] + "]"); } public void after(JoinPoint joinPoint) { System.out.println("after: " + "[x=" + joinPoint.getArgs()[0] + ", y=" + joinPoint.getArgs()[1] + "]"); } }
4. 添加配置文件
<?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" xmlns:context="http://www.springframework.org/schema/context" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!--配置bean--> <bean id="pureCalculator" class="xyz.huning.spring4.aop.calculator.PureCalculator"></bean> <!--配置切面bean--> <bean id="calculatorValidateAspect" class="xyz.huning.spring4.aop.calculator.aopimpl.xml.CalculatorValidateAspect"></bean> <bean id="calculatorLogAspect" class="xyz.huning.spring4.aop.calculator.aopimpl.xml.CalculatorLogAspect"></bean> <!--配置AOP--> <aop:config> <!--配置切点表达式--> <aop:pointcut expression="execution(public double xyz.huning.spring4.aop.calculator.PureCalculator.*(..))" id="pointcut"/> <!--配置切面及通知--> <aop:aspect ref="calculatorValidateAspect" order="1"> <aop:before method="validate" pointcut-ref="pointcut"/> </aop:aspect> <aop:aspect ref="calculatorLogAspect" order="2"> <aop:before method="before" pointcut-ref="pointcut"/> <aop:after method="after" pointcut-ref="pointcut"/> </aop:aspect> </aop:config> </beans>
5. 添加测试类
package xyz.huning.spring4.aop.calculator.aopimpl.xml; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import xyz.huning.spring4.aop.calculator.ICalculator; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("calculator-aop-xml.xml"); ICalculator calc = ctx.getBean("pureCalculator", ICalculator.class); calc.add(10, 20); calc.sub(30, 10); calc.mul(10, 10); calc.div(20, 10); ((ClassPathXmlApplicationContext)ctx).close(); } }
6. 执行结果
相关推荐
Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 ...
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
下面将详细介绍Spring AOP的注解方式和XML配置方式。 ### 注解方式 #### 1. 定义切面(Aspect) 在Spring AOP中,切面是包含多个通知(advisors)的类。使用`@Aspect`注解标记切面类,例如: ```java @Aspect ...
赠送jar包:spring-aop-5.3.10.jar; 赠送原API文档:spring-aop-5.3.10-javadoc.jar; 赠送源代码:spring-aop-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.10.pom; 包含翻译后的API文档:spring...
开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...
赠送jar包:spring-aop-5.3.12.jar; 赠送原API文档:spring-aop-5.3.12-javadoc.jar; 赠送源代码:spring-aop-5.3.12-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.12.pom; 包含翻译后的API文档:spring...
Spring AOP 是 Spring 框架中的一部分,它提供了一种实现 AOP 的方式。 在 Spring AOP 中,主要有两个概念:Aspect 和 Advice。Aspect 是一个模块,它封装了一个或者多个 Advice。Advice 是一个函数,它提供了某种...
以上就是使用XML配置实现Spring AOP的基本步骤。通过这种方式,我们可以灵活地控制通知的执行时机,并将关注点分离,使代码更清晰,更易于维护。 在实际项目中,还可以结合Spring的其他功能,如自动代理创建、注解...
赠送jar包:spring-aop-5.3.15.jar; 赠送原API文档:spring-aop-5.3.15-javadoc.jar; 赠送源代码:spring-aop-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.15.pom; 包含翻译后的API文档:spring...
赠送jar包:spring-aop-5.3.15.jar; 赠送原API文档:spring-aop-5.3.15-javadoc.jar; 赠送源代码:spring-aop-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.15.pom; 包含翻译后的API文档:spring...
赠送jar包:spring-aop-5.3.7.jar; 赠送原API文档:spring-aop-5.3.7-javadoc.jar; 赠送源代码:spring-aop-5.3.7-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.7.pom; 包含翻译后的API文档:spring-aop...
赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...
org.springframework.aop-sources-3.0.5.release.jar
在Spring框架中,AOP(面向切面编程)是一个核心特性,它允许开发者将关注点分离,实现代码的模块化和解耦。本篇将深入探讨为何在实际开发中会选用Spring AOP,并通过回顾其基本概念、工作原理以及如何在项目中应用...
赠送jar包:spring-aop-5.3.7.jar; 赠送原API文档:spring-aop-5.3.7-javadoc.jar; 赠送源代码:spring-aop-5.3.7-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.7.pom; 包含翻译后的API文档:spring-aop...
**Spring AOP XML方式配置通知** 在Java世界中,Spring框架是广泛应用的IoC(Inversion of Control)和AOP(Aspect Oriented Programming)容器。AOP允许开发者定义“方面”,这些方面可以封装关注点,如日志、事务...
赠送jar包:spring-aop-5.3.10.jar;赠送原API文档:spring-aop-5.3.10-javadoc.jar;赠送源代码:spring-aop-5.3.10-sources.jar;赠送Maven依赖信息文件:spring-aop-5.3.10.pom;包含翻译后的API文档:spring-aop...
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
赠送jar包:spring-aop-5.0.8.RELEASE.jar; 赠送原API文档:spring-aop-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.8.RELEASE.pom;...
这篇教程将详细讲解如何通过Spring的配置文件来实现AOP。 一、理解AOP概念 AOP的核心思想是将分散在各个模块中的交叉性代码(如日志、事务处理)抽取出来,形成独立的切面,以便于复用和维护。它提供了一种模块化的...