天天看點

Spring(十一)之AOP

Spring 架構的一個關鍵元件是面向方面的程式設計(AOP)架構。面向方面的程式設計需要把程式邏輯分解成不同的部分稱為所謂的關注點。跨一個應用程式的多個點的功能被稱為橫切關注點,這些橫切關注點在概念上獨立于應用程式的業務邏輯。有各種各樣的常見的很好的方面的例子,如日志記錄、審計、聲明式事務、安全性和緩存等。

在 OOP 中,關鍵單元子產品度是類,而在 AOP 中單元子產品度是方面。依賴注入幫助你對應用程式對象互相解耦和 AOP 可以幫助你從它們所影響的對象中對橫切關注點解耦。AOP 是像程式設計語言的觸發物,如 Perl,.NET,Java 或者其他。

Spring AOP 子產品提供攔截器來攔截一個應用程式,例如,當執行一個方法時,你可以在方法執行之前或之後添加額外的功能。

Spring(十一)之AOP
Spring(十一)之AOP

Spring中基于AOP的XML架構

示例:

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

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

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

(4)編寫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>      

(5)運作MainApp.java中的main方法

Spring(十一)之AOP