位置:首頁 > Java技術 > Spring > Spring基於@AspectJ的AOP

Spring基於@AspectJ的AOP

@ AspectJ是指聲明方麵的風格注釋的使用Java 5注釋普通的Java類。對@ AspectJ支持由包括您基於XML Schema的配置文件裡麵的下列元素啟用。

<aop:aspectj-autoproxy/>

您還需要在以下應用程序的類路徑中的AspectJ庫。這些庫可以在AspectJ的安裝的'lib'目錄,可以從網上下載他們.

  • aspectjrt.jar

  • aspectjweaver.jar

  • aspectj.jar

聲明一個切麵

方麵類是像任何其他普通的bean,並可能有方法和字段,就像任何其他類,但他們將被標注了@Aspect 如下: 

package org.xyz;

import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AspectModule {

}

他們將在XML中進行配置像任何其他的bean,如下所示:

<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.yiibai下執行getName()方法在Student類: 

import org.aspectj.lang.annotation.Pointcut;

@Pointcut("execution(* com.yiibai.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的基礎概念提到,讓我們寫這將實現幾個建議的一個例子。寫我們的例子中有一些建議,我們按照下麵的步驟來創建一個Spring應用程序:

步驟 描述
1 Create a project with a name SpringExample and create a package com.yiibai under the src folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Add Spring AOP specific libraries aspectjrt.jar, aspectjweaver.jar and aspectj.jar in the project.
4 Create Java classes LoggingStudent and MainApp under the com.yiibai package.
5 Create Beans configuration file Beans.xml under the src folder.
6 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

這裡是Logging.java文件的內容。這實際上是方麵模塊的一個示例,它定義的方法被調用的各個點。

package com.yiibai;

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.yiibai.*.*(..))")
   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.yiibai;

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.yiibai;

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.yiibai.Student">
      <property name="name"  value="Zara" />
      <property name="age"  value="11"/>      
   </bean>

   <!-- Definition for logging aspect -->
   <bean id="logging" class="com.yiibai.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