使用 Spring 的基于 XML 模式的 AOP
要使用本节中描述的 AOP 命名空间标签,您需要按照描述导入 springAOP 模式 −
<?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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-- bean definition & AOP specific configuration --> </beans>
您还需要在应用程序的 CLASSPATH 中包含以下 AspectJ 库。 这些库位于 AspectJ 安装的"lib"目录中,否则您可以从 Internet 下载它们。
- aspectjrt.jar
- aspectjweaver.jar
- aspectj.jar
- aopalliance.jar
声明一个方面
aspect 使用 <aop:aspect> 元素声明,支持 bean 使用 ref 属性引用,如下所示 −
<aop:config> <aop:aspect id = "myAspect" ref = "aBean"> ... </aop:aspect> </aop:config> <bean id = "aBean" class = "..."> ... </bean>
这里"aBean"将被配置和依赖注入,就像您在前面章节中看到的任何其他 Spring bean 一样。
声明切入点
切入点 有助于确定要使用不同建议执行的感兴趣的连接点(即方法)。 在使用基于 XML Schema 的配置时,切入点将定义如下 −
<aop:config> <aop:aspect id = "myAspect" ref = "aBean"> <aop:pointcut id = "businessService" expression = "execution(*com.xyz.myapp.service.*.*(..))"/> ... </aop:aspect> </aop:config> <bean id = "aBean" class = "..."> ... </bean>
以下示例定义了一个名为"businessService"的切入点,它将匹配 com.tutorialspoint 包下 Student 类中可用的 getName() 方法的执行 −
<aop:config> <aop:aspect id = "myAspect" ref = "aBean"> <aop:pointcut id = "businessService" expression = "execution(*com.tutorialspoint.Student.getName(..))"/> ... </aop:aspect> </aop:config> <bean id = "aBean" class = "..."> ... </bean>
声明建议
您可以使用 <aop:{ADVICE NAME}> 元素在 <aop:aspect> 中声明五个建议中的任何一个,如下所示 −
<aop:config> <aop:aspect id = "myAspect" ref = "aBean"> <aop:pointcut id = "businessService" expression = "execution(* com.xyz.myapp.service.*.*(..))"/> <!-- a before advice definition --> <aop:before pointcut-ref = "businessService" method = "doRequiredTask"/> <!-- an after advice definition --> <aop:after pointcut-ref = "businessService" method = "doRequiredTask"/> <!-- an after-returning advice definition --> <!--The doRequiredTask method must have parameter named retVal --> <aop:after-returning pointcut-ref = "businessService" returning = "retVal" method = "doRequiredTask"/> <!-- an after-throwing advice definition --> <!--The doRequiredTask method must have parameter named ex --> <aop:after-throwing pointcut-ref = "businessService" throwing = "ex" method = "doRequiredTask"/> <!-- an around advice definition --> <aop:around pointcut-ref = "businessService" method = "doRequiredTask"/> ... </aop:aspect> </aop:config> <bean id = "aBean" class = "..."> ... </bean>
您可以使用相同的 doRequiredTask 或不同的方法来获得不同的建议。 这些方法将被定义为切面模块的一部分。
基于 XML 模式的 AOP 示例
为了理解上述与基于 XML Schema 的 AOP 相关的概念,让我们编写一个示例来实现一些建议。 用一些建议来写我们的例子,让我们有一个可以工作的 Eclipse IDE,并按照以下步骤创建一个 Spring 应用程序 −
步骤 | 描述 |
---|---|
1 | 创建一个名为 SpringExample 的项目,并在创建的项目中的 src 文件夹下创建一个包 com.tutorialspoint。 |
2 | 使用 Add External JARs 选项添加所需的 Spring 库,如 Spring Hello World 示例 一章中所述。 |
3 | 在项目中添加 Spring AOP 特定库 aspectjrt.jar、aspectjweaver.jar 和 aspectj.jar。 |
4 | 在 com.tutorialspoint 包下创建 Java 类 Logging、Student 和 MainApp。 |
5 | 在 src 文件夹下创建 Beans 配置文件 Beans.xml。 |
6 | 最后一步是创建所有 Java 文件和 Bean 配置文件的内容,然后运行应用程序,如下所述。 |
这是 Logging.java 文件的内容。 这实际上是一个切面模块的示例,它定义了要在各个点调用的方法。
package com.tutorialspoint; public class Logging { /** * This is the method which I would like to execute * before a selected method execution. */ public void beforeAdvice(){ System.out.println("Going to setup student profile."); } /** * This is the method which I would like to execute * after a selected method execution. */ public void afterAdvice(){ System.out.println("Student profile has been setup."); } /** * This is the method which I would like to execute * when any method returns. */ public void afterReturningAdvice(Object retVal) { System.out.println("Returning:" + retVal.toString() ); } /** * This is the method which I would like to execute * if there is an exception raised. */ public void AfterThrowingAdvice(IllegalArgumentException ex){ System.out.println("There has been an exception: " + ex.toString()); } }
以下是 Student.java 文件的内容
package com.tutorialspoint; public class Student { private Integer age; private String name; public void setAge(Integer age) { this.age = age; } public Integer getAge() { System.out.println("Age : " + age ); return age; } public void setName(String name) { this.name = name; } public String getName() { System.out.println("Name : " + name ); return name; } public void printThrowException(){ System.out.println("Exception raised"); throw new IllegalArgumentException(); } }
以下是 MainApp.java 文件的内容
package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Student student = (Student) context.getBean("student"); student.getName(); student.getAge(); student.printThrowException(); } }
以下是配置文件Beans.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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <aop:config> <aop:aspect id = "log" ref = "logging"> <aop:pointcut id = "selectAll" expression = "execution(* com.tutorialspoint.*.*(..))"/> <aop:before pointcut-ref = "selectAll" method = "beforeAdvice"/> <aop:after pointcut-ref = "selectAll" method = "afterAdvice"/> <aop:after-returning pointcut-ref = "selectAll" returning = "retVal" method = "afterReturningAdvice"/> <aop:after-throwing pointcut-ref = "selectAll" throwing = "ex" method = "AfterThrowingAdvice"/> </aop:aspect> </aop:config> <!-- Definition for student bean --> <bean id = "student" class = "com.tutorialspoint.Student"> <property name = "name" value = "Zara" /> <property name = "age" value = "11"/> </bean> <!-- Definition for logging aspect --> <bean id = "logging" class = "com.tutorialspoint.Logging"/> </beans>
完成创建源和 bean 配置文件后,让我们运行应用程序。 如果您的应用程序一切正常,它将打印以下消息 −
Going to setup student profile. Name : Zara Student profile has been setup. Returning:Zara Going to setup student profile. Age : 11 Student profile has been setup. Returning:11 Going to setup student profile. Exception raised Student profile has been setup. There has been an exception: java.lang.IllegalArgumentException ..... other exception content
上面定义的 <aop:pointcut> 选择了com.tutorialspoint包下定义的所有方法。让我们假设,您想在特定方法之前或之后执行您的建议,您可以定义您的切入点,通过将切入点定义中的星号 (*) 替换为实际的类和方法名称来缩小执行范围。 下面是一个修改过的 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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <aop:config> <aop:aspect id = "log" ref = "logging"> <aop:pointcut id = "selectAll" expression = "execution(* com.tutorialspoint.Student.getName(..))"/> <aop:before pointcut-ref = "selectAll" method = "beforeAdvice"/> <aop:after pointcut-ref = "selectAll" method = "afterAdvice"/> </aop:aspect> </aop:config> <!-- Definition for student bean --> <bean id = "student" class = "com.tutorialspoint.Student"> <property name = "name" value = "Zara" /> <property name = "age" value = "11"/> </bean> <!-- Definition for logging aspect --> <bean id = "logging" class = "com.tutorialspoint.Logging"/> </beans>
如果您执行带有这些配置更改的示例应用程序,它将打印以下消息 −
Going to setup student profile. Name : Zara Student profile has been setup. Age : 11 Exception raised ..... other exception content