Spring AOP基於注解的Before Advice

@Before是一種通知類型,可以確保在方法執行之前運行通知。 以下是@Before通知(advice)的語法:

語法

@Pointcut("execution(* com.zaixian.Student.getName(..))")
private void selectGetName(){}

@Before("selectGetName()")
public void beforeAdvice(){
   System.out.println("Going to setup student profile.");
}

在上面的語法示例中 -

  • @Pointcut - 將函數標記為切入點
  • @Before - 將函數標記為在切入點覆蓋的方法之前執行的通知。
  • execution( expression ) - 涵蓋應用通知的方法的運算式。

要瞭解上面提到的連接點(JoinPoint)和切入點(PointCut)相關的概念,我們寫一個Spring AOP基於注解的應用例子,它將使用基於注解配置實現通知。打開並使用Eclipse IDE,並按照以下步驟創建一個Spring應用程式:

  1. 更新在Spring AOP應用實例中創建過的Student專案。
  2. 更新bean配置並運行應用程式,如下所述。

整個專案的目錄結構如下所示 -

下麵是Logging.java檔的內容。 這實際上是一個Aspect模組的樣本,它定義了在各個點被調用的方法。

package com.zaixian;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;

@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.zaixian.Student.getName(..))")
   private void selectGetName(){}

   /**
    * This is the method which I would like to execute
    * before a selected method execution.
    */
   @Before("selectGetName()")
   public void beforeAdvice(){
      System.out.println("[beforeAdvice]Going to setup student profile.");
   }
}

以下是Student.java檔的內容:

package com.zaixian;

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

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();
   }
}

以下是配置檔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.zaixian.Student">
      <property name="name"  value="minsu" />
      <property name="age"  value="25"/>
   </bean>

   <!-- Definition for logging aspect -->
   <bean id="logging" class="com.zaixian.Logging"/>

</beans>

運行專案

完成創建源代碼和配置檔後,運行應用程式。右鍵單擊應用程式中的MainApp.java,並使用運行方式作為Java應用程式命令。 如果您的應用程式一切正常,這將列印以下消息:

[beforeAdvice]Going to setup student profile.
Name : minsu
Age : 25

這裏簡單解釋一下上面定義的@Pointcut,它使用一個運算式來選擇在com.zaixian包中定義的類中定義的方法getName()@Before通知使用上面定義的切入點作為參數。 在上述切入點覆蓋的每個方法之前,都將調用beforeAdvice()方法。


上一篇: Spring AOP基於注解的切入點 下一篇: Spring AOP基於注解的After Advice