@AspectJ 基于 Spring 的 AOP
@AspectJ 指的是一种将切面声明为使用 Java 5 注解进行注解的常规 Java 类的风格。 通过在基于 XML 模式的配置文件中包含以下元素来启用 @AspectJ 支持。
<aop:aspectj-autoproxy/>
在应用程序的类路径中,您还需要以下 AspectJ 库。 这些库在 AspectJ 安装的"lib"目录中可用,否则您可以从 Internet 下载它们。
- aspectjrt.jar
- aspectjweaver.jar
- aspectj.jar
- aopalliance.jar
声明切面
Aspects 类与任何其他普通 bean 一样,并且可以像任何其他类一样具有方法和字段,不同之处在于它们将使用 @Aspect 进行注解,如下所示 −
package org.xyz;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class AspectModule {
}
它们将像任何其他 bean 一样在 XML 中配置,如下所示 −
<bean id = "myAspect" class = "org.xyz.AspectModule"> <!-- configure properties of aspect here as normal --> </bean>
声明切入点
切入点 有助于确定要使用不同建议执行的感兴趣的连接点(即方法)。 在使用基于@AspectJ 的配置时,切入点声明有两个部分 −
一个切入点表达式,它准确地确定了我们对哪些方法执行感兴趣。
一个切入点签名,包括名称和任意数量的参数。 该方法的实际主体是无关紧要的,实际上应该是空的。
以下示例定义了一个名为"businessService"的切入点,它将匹配 com.xyz.myapp.service 包下的类中可用的每个方法的执行 −
import org.aspectj.lang.annotation.Pointcut;
@Pointcut("execution(* com.xyz.myapp.service.*.*(..))") // expression
private void businessService() {} // signature
以下示例定义了一个名为"getname"的切入点,它将匹配 com.tutorialspoint 包下 Student 类中可用的 getName() 方法的执行 −
import org.aspectj.lang.annotation.Pointcut;
@Pointcut("execution(* com.tutorialspoint.Student.getName(..))")
private void getname() {}
声明建议
您可以使用代码片段中给出的 @{ADVICE-NAME} 注解声明五个建议中的任何一个。 这假设您已经定义了切入点签名方法 businessService() −
@Before("businessService()")
public void doBeforeTask(){
...
}
@After("businessService()")
public void doAfterTask(){
...
}
@AfterReturning(pointcut = "businessService()", returning = "retVal")
public void doAfterReturnningTask(Object retVal) {
// you can intercept retVal here.
...
}
@AfterThrowing(pointcut = "businessService()", throwing = "ex")
public void doAfterThrowingTask(Exception ex) {
// you can intercept thrown exception here.
...
}
@Around("businessService()")
public void doAroundTask(){
...
}
您可以为任何建议定义内联切入点。 以下是为 before 通知定义内联切入点的示例 −
@Before("execution(* com.xyz.myapp.service.*.*(..))")
public doBeforeTask(){
...
}
基于@AspectJ 的 AOP 示例
为了理解上面提到的与基于@AspectJ 的 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;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
@Aspect
public class Logging {
/** Following is the definition for a pointcut to select
* all the methods available. So advice will be called
* for all the methods.
*/
@Pointcut("execution(* com.tutorialspoint.*.*(..))")
private void selectAll(){}
/**
* This is the method which I would like to execute
* before a selected method execution.
*/
@Before("selectAll()")
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.
*/
@After("selectAll()")
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.
*/
@AfterReturning(pointcut = "selectAll()", returning = "retVal")
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 by any method.
*/
@AfterThrowing(pointcut = "selectAll()", throwing = "ex")
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:aspectj-autoproxy/>
<!-- 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

