Spring Core – Phần 5: Spring AOP là gì? code ví dụ với Spring AOP

Related Articles

Spring Core – Phần 5 : Spring AOP là gì ? code ví dụ với Spring AOP

Aspect Oriented Programming (AOP) là 1 kỹ thuật lập trình dùng để tách logic chương trình thành các phần riêng biệt…

Trong Spring AOP, có 4 loại advice được tương hỗ :

  • Before advice: chạy trước khi method được thực thi
  • After returning advice: Chạy sau khi method trả về một kết quả
  • After throwing adivce: Chạy khi method ném ra một exception
  • Around advice: Chạy khi method được thực thi (Bao gồm cả 3 loại advice trên)

Ở bài trước mình đã trình làng rõ AOP là gì, công dụng và đặc thù của nó nên trong bài này mình sẽ đa phần tập trung chuyên sâu vào cách thực thi AOP trong Spring .

Nhắc lại một chút ít về một số ít khái nhiệm và thuật ngữ trong AOP với Spring ( Phần này hơi trừu tượng một chút ít, những bạn so sánh với ví dụ bên dưới để hiểu hơn )

  • Join point: là các điểm trong chương trình ví dụ điểm thực thi method (method execution), điểm xử lý excpetion, field access… Spring chỉ hỗ trợ method execution join point
  • Advice: một hành động thực hiện ở joint point
  • Pointcut: Là expression language giúp khớp nối với joint point
  • Introduction: Cho phép introduce các new interface tới bất kì object adviced nào.
  • Target Object: Object sẽ được adviced
  • Aspect: là một class bao gồm các advice và các joint point
  • Interceptor: là một aspect chỉ có duy nhất một advice
  • AOP Proxy: dùng để cài đặt các Aspect
  • Weaving: là tiến trình nối các aspect với các object, types để tạo nên advised object.

Spring Core - Phần 5: Spring AOP là gì? code ví dụ với Spring AOP

Ví dụ kinh điểm với AOP mà ta hay dùng đó là chương trình thực thi log .

Bây giờ mình sẽ tạo 1 class với những method, sau đó vận dụng AOP để triển khai log những method của class theo cả 4 loại advice :

Khai báo những thư viện để sử dụng applicationContext, bean, aop trong spring :

 org.springframework spring-beans 4.3.13.RELEASE org.springframework spring-context 4.3.13.RELEASE org.springframework spring-aop 4.3.13.RELEASE

Tạo class Hello. java với 3 method :

public class Hello { public void method1() { System.out.println("+++++++++++++++++++++++++++++++"); System.out.println("method 1"); } public void method2() { System.out.println("+++++++++++++++++++++++++++++++"); System.out.println("method 2"); } public void method3() { System.out.println("+++++++++++++++++++++++++++++++"); System.out.println("method 3"); throw new IllegalArgumentException(); }
}

applicationContext. xml

 

Demo :

public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Hello hello = (Hello) context.getBean("hello"); hello.method1(); hello.method2();
}

Kết quả :

+++++++++++++++++++++++++++++++
method 1
+++++++++++++++++++++++++++++++
method 2

Áp dụng before advice

Tạo class DemoBeforeAdvice.java định nghĩa hành động trước khi cách method của class Hello được chạy:

public class DemoBeforeAdvice implements MethodBeforeAdvice { public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("before method: " + method.getName()); }
}

MethodBeforeAdvice : là một aspect đồng thời cũng là một Interceptor vì nó chỉ có 1 advice

DemoBeforeService : là một AOP proxy, nó thiết lập lại aspect là MethodBeforeAdvice

aplicationContext. xml

       demoBeforeAdvice   

target object sẽ là đối tượng người dùng hello .

Demo :

public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Hello hello = (Hello) context.getBean("helloProxy"); hello.method1(); hello.method2();
}

Kết quả :

before method: method1
+++++++++++++++++++++++++++++++
method 1
before method: method2
+++++++++++++++++++++++++++++++
method 2

Tương tự mình sẽ tạo những proxy triển khai thiết lập After returning advice, After throwing adivce, Around advice :

import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class DemoAfterAdvice implements AfterReturningAdvice { public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("after method: " + method.getName()); }
}
import org.springframework.aop.ThrowsAdvice;
public class DemoThrowAdvice implements ThrowsAdvice { public void afterThrowing(IllegalArgumentException e) throws Throwable { System.out.println("throw advice method: " ); }
}
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class DemoAroundAdvice implements MethodInterceptor { public Object invoke(MethodInvocation invocation) throws Throwable { // same with MethodBeforeAdvice System.out.println("around - before method: " + invocation.getMethod().getName()); try { // proceed to original method call Object result = invocation.proceed(); // same with AfterReturningAdvice System.out.println("around - before method: " + invocation.getMethod().getName()); return result; } catch (IllegalArgumentException e) { // same with ThrowsAdvice System.out.println("around - throw advice method: " + invocation.getMethod().getName()); throw e; } }
}

applicationContext. xml

          demoBeforeAdvice demoAfterAdvice demoThrowAdvice demoAroundAdvice   

Demo :

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp { public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Hello hello = (Hello) context.getBean("helloProxy"); hello.method1(); System.out.println("n"); hello.method2(); System.out.println("n"); hello.method3(); }
}

Kết quả:

before method: method1
around - before method: method1
+++++++++++++++++++++++++++++++
method 1
around - before method: method1
after method: method1
before method: method2
around - before method: method2
+++++++++++++++++++++++++++++++
method 2
around - before method: method2
after method: method2
before method: method3
around - before method: method3
+++++++++++++++++++++++++++++++
method 3
around - throw advice method: method3
throw advice method:
Exception in thread "main" java.lang.IllegalArgumentException

Okay, Done !

Các bạn hoàn toàn có thể tải về code ví dụ trên tại đây

References : https://docs.spring.io/spring/docs/3.0.x/reference/aop.html

More on this topic

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Advertismentspot_img

Popular stories