Spring AOP - คำอธิบายประกอบตามคำแนะนำ
@Aroundเป็นประเภทคำแนะนำซึ่งช่วยให้มั่นใจได้ว่าคำแนะนำสามารถทำงานก่อนและหลังการดำเนินการตามวิธีการ ต่อไปนี้เป็นไวยากรณ์ของคำแนะนำ @Around
ไวยากรณ์
@PointCut("execution(* com.tutorialspoint.Student.getAge(..))")
private void selectGetName(){}
@Around("selectGetAge()")
public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint){
   System.out.println("Around advice");
   Object[] args = jp.getArgs();
   if(args.length>0){
      System.out.print("Arguments passed: " );
      for (int i = 0; i < args.length; i++) {
         System.out.print("arg "+(i+1)+": "+args[i]);
      }
   }
   Object result = jp.proceed(args);
   System.out.println("Returning " + result);
   return result.toString();   
} 
    ที่ไหน
@PointCut - ทำเครื่องหมายฟังก์ชันเป็น PointCut
execution( expression ) - นิพจน์ครอบคลุมวิธีการที่จะนำคำแนะนำไปใช้
@Around - ทำเครื่องหมายฟังก์ชันเป็นคำแนะนำที่จะดำเนินการก่อนที่เมธอดจะครอบคลุมโดย PointCut
หากต้องการทำความเข้าใจแนวคิดดังกล่าวข้างต้นที่เกี่ยวข้องกับ @Around Advice ให้เราเขียนตัวอย่างที่จะนำไปใช้ @Around Advice ในการเขียนตัวอย่างของเราพร้อมคำแนะนำเล็กน้อยให้เรามี Eclipse IDE ที่ใช้งานได้และใช้ขั้นตอนต่อไปนี้เพื่อสร้างแอปพลิเคชัน Spring -
| ขั้นตอน | คำอธิบาย | 
|---|---|
| 1 | อัพเดทโครงการนักศึกษาสร้างขึ้นภายใต้บทที่ฤดูใบไม้ผลิ AOP - การประยุกต์ใช้ | 
| 2 | อัปเดตการกำหนดค่า bean และเรียกใช้แอปพลิเคชันตามที่อธิบายด้านล่าง | 
ต่อไปนี้เป็นเนื้อหาของ Logging.javaไฟล์. นี่คือตัวอย่างของโมดูลด้านข้างซึ่งกำหนดวิธีการที่จะเรียกใช้ในจุดต่างๆ
package com.tutorialspoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.PointCut;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.ProceedingJoinPoint; 
@Aspect
public class Logging {
   /** Following is the definition for a PointCut to select
    *  all the methods available. So advice will be called
    *  for all the methods.
    */
   @PointCut("execution(* com.tutorialspoint.Student.getAge(..))")
   private void selectGetAge(){}
   /** 
    * This is the method which I would like to execute
    * around a selected method execution.
    */
   @Around("selectGetAge()")
   public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
      System.out.println("Around advice");
      Object[] args = proceedingJoinPoint.getArgs();
      if(args.length>0){
         System.out.print("Arguments passed: " );
         for (int i = 0; i < args.length; i++) {
            System.out.print("arg "+(i+1)+": "+args[i]);
         }
      }
      Object result = proceedingJoinPoint.proceed(args);
      System.out.println("Returning " + result);
   }  
} 
    ต่อไปนี้เป็นเนื้อหาของไฟล์ 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();
   }
} 
    ต่อไปนี้เป็นเนื้อหาของไฟล์ 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.getAge();     
   }
} 
    ต่อไปนี้เป็นไฟล์กำหนดค่า 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:aspectj-autoproxy/>
   <!-- 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> 
    เรียกใช้โครงการ
เมื่อคุณสร้างซอร์สและไฟล์การกำหนดค่าเสร็จแล้วให้เรียกใช้แอปพลิเคชันของคุณ คลิกขวาที่ MainApp.java ในแอปพลิเคชันของคุณและใช้งานrun as Java Applicationคำสั่ง หากทุกอย่างเรียบร้อยดีกับแอปพลิเคชันของคุณแอปพลิเคชันของคุณจะพิมพ์ข้อความต่อไปนี้
Around advice
Age : 11
Returning 11