Giới thiệu nội dung bài viết

Chào các em ,chủ để hôm nay chúng ta sẽ tìm hiểu về AOP Annotation có ý nghĩa là gì nhé .

1. Spring AOP Annotation là gì

Trong bài AOP advise chúng ta sử dụng xml để cấu hình cho advise, pointcut và around. Hôm nay chúng ta sẽ sử dụng annotation để làm AOP như

Đang xem: Spring aop là gì, spring aop và aspectj

Around

2. Cấu hình file pom

Chúng ta thêm các thư viện AspectJrt và aspecttools vào trong dự án để sử dụng aop

3. Tạo Model

Trong ví dụ hôm nay chúng ta sẽ làm việc với Employee. Chúng ta có lớp Employee như sau

12345678910111213141516171819 package com.gocnhintangphat.com;public class Employee {private String name;public String getName() {return name;}
Loggablepublic void setName(String nm) {this.name=nm;}public void throwException(){throw new RuntimeException(“Dummy Exception”);}}

Chúng ta thấy có annotation là
Loggable thì cái annotation này dùng ghi log và do chúng ta tự định nghĩa ra. Phần này anh sẽ nói rõ hơn trong phần 10 dưới đây.

3. Tạo Service

Chúng ta sẽ tạo Class EmployeeService để lấy Employee như sau

Aspect trong đó ta định nghĩa phương thức nào cần hook trước khi chạy. Trong ví dụ này chúng ta áp dụng khi chạy phương thức getName

Một Aspect Class sẽ được đánh dấu bằng
Before được sử dụng để chạy trước khi method getName được gọi

12345678910111213141516 import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;
Before(“execution(public String getName())”)public void getNameAdvice(){System.out.println(“Executing Advice on getName()”);}
Before(“execution(*.com.gocnhintangphat.com.*.get*())”)public void getAllAdvice(){System.out.println(“Service method getter called”);}}

6. Ví dụ với AOP Pointcut MethodChúng ta sử dụng phương thức nào sẽ được chạy AOP hoặc tất cả phương thức được chạy bằng sử dụng within

1234567891011121314151617181920212223242526 Before(“getNamePointcut()”)public void loggingAdvice(){System.out.println(“Executing loggingAdvice on getName()”);}
Before(“getNamePointcut()”)public void secondAdvice(){System.out.println(“Executing secondAdvice on getName()”);}
Before(“allMethodsPointcut()”)public void allServiceMethodsAdvice(){System.out.println(“Before executing service method”);}//Pointcut to execute on all the methods of classes in a package
Pointcut(“within(com.gocnhintangphat.com.service.*)”)public void allMethodsPointcut(){}}

7. Ví dụ với AOP JoinPoint và AdviceChúng ta sử dụng AOP cho các phương thức là set có trong Employee model

123456789101112131415161718192021 import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;
Before(“execution(public void com.gocnhintangphat.com.model..set*(*))”)public void loggingAdvice(JoinPoint joinPoint){System.out.println(“Before running loggingAdvice on method=”+joinPoint.toString());System.out.println(“Arguments Passed=” + Arrays.toString(joinPoint.getArgs()));}//Advice arguments, will be applied to bean methods with single String argument
Before(“args(name)”)public void logStringArguments(String name){System.out.println(“String argument passed=”+name);}}

EmployeeAfterAspect

8. Ví dụ với AOP After Aspect

Chúng ta sử dụng

Xem thêm: What Is The Meaning Of “That One / This One Là Gì Trong Tiếng Anh?

After để chạy AOP khi method đã chạy xong

12345678910111213141516171819202122232425 import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;
After(“args(name)”)public void logStringArguments(String name){System.out.println(“Running After Advice. String argument passed=”+name);}
AfterThrowing(“within(com.gocnhintangphat.com.Employee)”)public void logExceptions(JoinPoint joinPoint){System.out.println(“Exception thrown in Employee Method=”+joinPoint.toString());}
AfterReturning(pointcut=”execution(* getName())”, returning=”returnString”)public void getNameReturningAdvice(String returnString){System.out.println(“getNameReturningAdvice executed. Returned String=”+returnString);}}

9. Ví dụ AOP Around Aspect

1234567891011121314151617181920 import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;
Around(“execution(* com.journaldev.spring.model.Employee.getName())”)public Object employeeAroundAdvice(ProceedingJoinPoint proceedingJoinPoint){System.out.println(“Before invoking getName() method”);Object value = null;try {value = proceedingJoinPoint.proceed();} catch (Throwable e) {e.printStackTrace();}System.out.println(“After invoking getName() method. Return value=”+value);return value;}}

10. Tự tạo annotation aop pointcutNhư các em thấy trong Module Employee ta thấy một annotation là
Loggable đây chính là annotation do chúng ta tự tạo ra bằng cách sau

12345 package com.gocnhintangphat.com;public
Loggable mới được chạy như sau

1234567891011 import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;
annotation(com.gocnhintangphat.com.Loggable)”)public void myAdvice(){System.out.println(“Executing myAdvice!!”);}}

*

Play

Mọi người hãy Subscribe kênh youtube dưới đây nhé để cập nhật các video mới nhất về kỹ thuật và kỹ năng mềm

*

*

Xem thêm: Chèo Thuyền Tiếng Anh Là Gì ? Chèo Thuyền Trong Tiếng Anh Là Gì

Về Tác giả Blog tập trung những kiến thức và những trải nghiệm của anh về ngành phần mềm. Nhằm giúp đỡ các bạn học sinh, sinh viên hiểu sâu hơn về nghề lập trình thông qua các kinh nghiệm thực tế mà anh làm trong các doanh nghiệp Âu, Mỹ và Nhật. Trong Blog này anh có chuyển đổi nội dung một số kiến thức từ các blog nổi tiếng bằng tiếng Anh sang tiếng Việt nhằm giúp mọi người hiểu được nguyên lý dễ dàng hơn.

Leave a Reply

Your email address will not be published. Required fields are marked *