Spring AOP-사용자 정의 주석

PointCut 표현식에 따라 어드바이스가 의도되지 않은 다른 빈에 적용되는 경우가있을 수 있습니다. 예를 들어, 다음 표현식을 고려하십시오.

execution(* com.tutorialspoint.*.getAge(..))

getAge () 메소드로 새로운 spring bean이 추가되고 의도하지 않았더라도 조언이 적용되기 시작합니다. 이를 달성하기 위해 사용자 지정 주석을 만들고 조언이 적용될 메서드에 주석을 달 수 있습니다.

@Before("@annotation(com.tutorialspoint.Loggable)")

위에서 언급 한 @Before Advice 관련 개념을 이해하기 위해 @Before Advice를 구현하는 예제를 작성해 보겠습니다. 몇 가지 조언으로 예제를 작성하기 위해 작동하는 Eclipse IDE를 준비하고 다음 단계를 사용하여 Spring 애플리케이션을 만듭니다.

단계 기술
1 Spring AOP-Application 장에서 만든 Student 프로젝트를 업데이트합니다 .
2 Bean 구성을 업데이트하고 아래 설명 된대로 애플리케이션을 실행하십시오.

다음은 내용입니다 Logging.java파일. 이것은 실제로 여러 지점에서 호출 될 메서드를 정의하는 aspect 모듈의 샘플입니다.

package com.tutorialspoint;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class Logging {

   /** 
    * This is the method which I would like to execute
    * before a selected method execution.
    */
   @Before("@annotation(com.tutorialspoint.Loggable)")
   public void beforeAdvice(){
      System.out.println("Going to setup student profile.");
   }  
}

다음은의 내용입니다 Loggable.java 파일-

package com.tutorialspoint;

public @interface Loggable {

}

다음은의 내용입니다 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;
   }
   
   @Loggable
   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.getName();
      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명령. 응용 프로그램에 문제가 없으면 다음 메시지가 인쇄됩니다.

Going to setup student profile.
Name : Zara
Age : 11