Spring aop là gì

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

Chào các em ,chủ để bây giờ chúng ta sẽ mày mò về AOP Annotation có chân thành và ý nghĩa là gì nhé .

1. Spring AOP Annotation là gì

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


Bạn đang xem: Spring aop là gì

Around

2. Cấu hình file pom

Chúng ta thêm các thư viện AspectJrt cùng aspecttools vào trong dự án công trình để áp dụng aop

3. Tạo Model

Trong ví dụ từ bây giờ chúng ta sẽ thao tác làm việc với Employee. Chúng ta có lớp Employee như sau

12345678910111213141516171819package com.detnhatrang.com.vn;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 tất cả annotation là
Loggable thì mẫu annotation này dùng ghi log cùng do chúng ta tự tư tưởng ra. Phần này anh sẽ nói rõ rộng trong phần 10 dưới đây.

3. Sản xuất Service

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

Aspect trong những số ấy ta định nghĩa cách làm nào đề xuất hook trước lúc chạy. Trong lấy ví dụ này họ áp dụng lúc chạy cách tiến hành getName

Một Aspect Class đang được lưu lại bằng
Before được áp dụng để chạy trước khi method getName được gọi

12345678910111213141516import 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.detnhatrang.com.vn.*.get*())")public void getAllAdvice()System.out.println("Service method getter called");}
6. Lấy ví dụ với AOP Pointcut MethodChúng ta sử dụng phương thức nào sẽ tiến hành chạy AOP hoặc toàn bộ 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 lớn execute on all the methods of classes in a package
Pointcut("within(com.detnhatrang.com.vn.service.*)")public void allMethodsPointcut()}
7. Ví dụ như với AOP JoinPoint với AdviceChúng ta sử dụng AOP cho những phương thức là set có trong Employee model

123456789101112131415161718192021import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;
Before("execution(public void com.detnhatrang.com.vn.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 lớn 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
After để chạy AOP khi method đang chạy xong

12345678910111213141516171819202122232425import 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;


Xem thêm: Doki Doki Literature Club Việt Hóa Download, Tải Game Doki Doki Literature Club! Apk 1

After("args(name)")public void logStringArguments(String name)System.out.println("Running After Advice. String argument passed="+name);
AfterThrowing("within(com.detnhatrang.com.vn.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. Lấy ví dụ AOP Around Aspect
1234567891011121314151617181920import 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 nên 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 ra bằng cách sau

12345package com.detnhatrang.com.vn;public
Loggable bắt đầu được chạy như sau

1234567891011import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;
annotation(com.detnhatrang.com.vn.Loggable)")public void myAdvice()System.out.println("Executing myAdvice!!");}

*
Play

Mọi bạn hãy Subscribe kênh youtube sau đây nhé để update các clip mới tốt nhất về nghệ thuật và khả năng mềm


*

*

*

*



Xem thêm: Hæ°Á»›Ng DẫN Lã¡I Xe Số Tự Đông Là Gì ? Ký Hiệu Trên Cần Số

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