@AspectJ based
@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>
宣布切入点
pointcut有助于确定要用不同建议执行的感兴趣的连接点(即方法)。 在使用基于@ AspectJ的配置时,切入点声明有两个部分 -
切入点表达式,确切地确定我们感兴趣的方法执行。
包含名称和任意数量参数的切入点签名。 该方法的实际主体是无关紧要的,实际上应该是空的。
以下示例定义了一个名为“businessService”的切入点,该切入点将与com.xyz.myapp.service包下的类中可用的每个方法的执行相匹配 -
import org.aspectj.lang.annotation.Pointcut;
@Pointcut("execution(* com.xyz.myapp.service.*.*(..))") // expression
private void businessService() {} // signature
以下示例定义了一个名为“getname”的切入点,该切入点将与cn.xnip包下的Student类中可用的getName()方法的执行相匹配 -
import org.aspectj.lang.annotation.Pointcut;
@Pointcut("execution(* cn.xnip.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("execution(* com.xyz.myapp.service.*.*(..))")
public doBeforeTask(){
...
}
@AspectJ基于AOP的例子
为了理解与基于@AspectJ的AOP相关的上述概念,让我们编写一个实现很少建议的例子。 要编写我们的示例并提供一些建议,让我们使用一个可用的Eclipse IDE并执行以下步骤来创建Spring应用程序 -
脚步 | 描述 |
---|---|
1 | 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包cn.xnip 。 |
2 | 使用Add External JARs选项添加所需的Spring库,如Spring Hello World Example章节中所述。 |
3 | 在项目中添加Spring AOP特定库aspectjrt.jar, aspectjweaver.jar和aspectj.jar 。 |
4 | 在cn.xnip包下创建Java类Logging , Student和MainApp 。 |
5 | 在src文件夹下创建Beans配置文件Beans.xml 。 |
6 | 最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述。 |
这是Logging.java文件的内容。 这实际上是方面模块的一个示例,它定义了在各个点调用的方法。
package cn.xnip;
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(* cn.xnip.*.*(..))")
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 cn.xnip;
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 cn.xnip;
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 = "cn.xnip.Student">
<property name = "name" value = "Zara" />
<property name = "age" value = "11"/>
</bean>
<!-- Definition for logging aspect -->
<bean id = "logging" class = "cn.xnip.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