JAVA/SPRING 2009. 5. 6. 11:14

@Aspect 어노테이션은 AspectJ 5버전에 새롭게 추가된 어노테이션으로, @Aspect 어노테이션을 사용하면 설정파일에 Advice 및 Pointcut등의 설정을 하지 않고도 자동으로 Advice를 적용 할 수 있습니다. 스프링2부터 이 어노테이션을 지원하고 있으며 설정파일에 <aop:aspectj-autoproxy> 태그를 설정파일에 추가해주면 @Aspect 어노테이션이 적용된 빈을 Aspect로 사용할 수 있게 됩니다. aop네임스페이스도 설정파일에 포함시켜야 합니다.

스프링인 액션 참고 자료.
설정파일

<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-2.0.xsd

       http://www.springframework.org/schema/aop 

      http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">



  <aop:aspectj-autoproxy />

  <bean class="chapter4.springidol.Audience" />

  ....


</beans>



어노테이션이 적용된 자바소스파일

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;


@Aspect

public class Audience {

public Audience() {

}


@Pointcut("execution(* *.perform(..))")

public void performance() {

}


@Before("performance()")

public void takeSeats() {

System.out.println("The audience is taking their seats.");

}


@Before("performance()")

public void turnOffCellPhones() {

System.out.println("The audience is turning off their cellphones");

}


@AfterReturning("performance()")

public void applaud() {

System.out.println("CLAP CLAP CLAP CLAP CLAP");

}


@AfterThrowing("performance()")

public void demandRefund() {

System.out.println("Boo! We want our money back!");

}

}



@Around어노테이션 사용시 주의점

@Around어노테이션 사용시에는 반드시 proceed()를 호출하는 부분이 들어가야 합니다. proceed()가 호출이되어야 원래 호출되야 할 메서드로 실행이 넘어갑니다. 이를위해 Around어드바이스가 적용된 메소드에서 ProceedingJoinPoint객체를 인자로 받도록 선언을 해 주어야 합니다.
posted by 나는너의힘
: