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

1. Spring AOP là gì?

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 hỗ trợ:

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 đã giới thiệu rõ AOP là gì, tác dụng và đặc điểm của nó nên trong bài này mình sẽ chủ yếu tập trung vào cách thực thi AOP trong Spring.

Bạn đang xem: Aop là gì

2. Code ví dụ với Spring AOP.

Xem thêm: Reason Core Security Là Gì, Download Reason Core Security 3

Nhắc lại một chút về một số 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, các bạn đối chiếu 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.

*

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

Xem thêm: Tử Vi Tuổi 1952 Năm 2021 – Tử Vi Tuổi Nhâm Thìn 2021

Bây giờ mình sẽ tạo 1 class với các method, sau đó áp dụng AOP để thực hiện log các method của class theo cả 4 loại advice:

Khai báo các 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 method2() public void method3() }

applicationContext.xml

Demo:

public static void main(String args) throws Exception

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 }

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ó cài đặt lại aspect là MethodBeforeAdvice

aplicationContext.xml

demoBeforeAdvice

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

Demo:

public static void main(String args) throws Exception

Kết quả:

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

Tương tự mình sẽ tạo các proxy thực hiện cài đặt After returning advice, After throwing adivce, Around advice:

import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; public class DemoAfterAdvice implements AfterReturningAdvice } import org.springframework.aop.ThrowsAdvice; public class DemoThrowAdvice implements ThrowsAdvice } import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class DemoAroundAdvice implements MethodInterceptor catch (IllegalArgumentException e) } }

 

applicationContext.xml

demoBeforeAdvice demoAfterAdvice demoThrowAdvice demoAroundAdvice

Demo:

import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp }

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 có thể download code ví dụ trên tại đây

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

Chuyên mục: Hỏi Đáp