- Spring 核心基础
- Spring - 首页
- Spring - 概览
- Spring - 架构
- Spring - 环境搭建
- Spring - Hello World 示例
- Spring - IoC 容器
- Spring - Bean 定义
- Spring - Bean 作用域
- Spring - Bean 生命周期
- Spring - Bean 后置处理器
- Spring - Bean 定义继承
- Spring - 依赖注入
- Spring - 注入内部 Bean
- Spring - 注入集合
- Spring - Bean 自动装配
- 基于注解的配置
- Spring - 基于 Java 的配置
- Spring - Spring 中的事件处理
- Spring - Spring 中的自定义事件
- Spring - 使用 Spring 框架的 AOP
- Spring - JDBC 框架
- Spring - 事务管理
- Spring - Web MVC 框架
- Spring - 使用 Log4J 进行日志记录
- Spring 常见问题解答
- Spring - 问题与解答
- Spring 有用资源
- Spring - 快速指南
- Spring - 有用资源
- Spring - 讨论
基于 XML Schema 的 Spring 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”目录中,或者您可以从互联网上下载它们。
- aspectjrt.jar
- aspectjweaver.jar
- aspectj.jar
- aopalliance.jar
声明一个切面
切面 使用 <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 模式配置时,切点将定义如下:
<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:aspect> 内使用 <aop:{ADVICE NAME}> 元素声明五种通知中的任何一种,如下所示:
<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 Schema 的 AOP 示例
为了理解与基于 XML Schema 的 AOP 相关的上述概念,让我们编写一个示例,该示例将实现一些通知。要使用一些通知编写我们的示例,让我们准备一个可用的 Eclipse IDE,并采取以下步骤创建一个 Spring 应用程序:
| 步骤 | 描述 |
|---|---|
| 1 | 创建一个名为 SpringExample 的项目,并在创建的项目中的 src 文件夹下创建一个包 com.tutorialspoint。 |
| 2 | 使用 添加外部 JAR 选项添加所需的 Spring 库,如 Spring Hello World 示例 章节中所述。 |
| 3 | 在项目中添加 Spring AOP 特定的库 aspectjrt.jar、aspectjweaver.jar 和 aspectj.jar。 |
| 4 | 在 com.tutorialspoint 包下创建 Java 类 Logging、Student 和 MainApp。 |
| 5 | 在 src 文件夹下创建 Bean 配置文件 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