Before Advice를 구현할 때에는 @Before 어노테이션을 사용

  - 대상 객체의 메서드 호출 전에 공통 기능을 실행

 

※ After Returning Advice를 구현하려면 @AfterReturning 어노테이션을 Advice 구현 메서드에 적용

  - 대상 객체의 메서드가 예외 없이 실행한 이후에 공통 기능을 실행

 

■ 대상 객체 및 호출되는 메서드에 대한 정보나 전달되는 파라미터에 대한 정보가 필요한 경우 org.aspectj.lang.JoinPoint를 파라미터로 추가

 

 

FaqBoardImpl.java

package sp.aop.service;

import org.springframework.stereotype.Component;

 

@Component("faqBoard")
public class FaqBoardImpl implements Board {

 @Override
 public String getBoardName() {
  // TODO Auto-generated method stub
  return "FAQ Board";
 }

 @Override
 public String checkBoard() {
  // TODO Auto-generated method stub
  return "N";
 }
}

 

NoticeBoardImpl.java

package sp.aop.service;

import org.springframework.stereotype.Component;

 

@Component("noticeBoard")
public class NoticeBoardImpl implements Board {

 @Override
 public String getBoardName() {
  // TODO Auto-generated method stub
  return "Notice Board";
 }

 @Override
 public String checkBoard() {
  // TODO Auto-generated method stub
  return "Y";
 }
}

 

BoardController.java

package sp.aop.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import sp.aop.service.Board;

 

public class BoardController extends
  org.springframework.web.servlet.mvc.AbstractController {

 

 @Resource(name="noticeBoard")
 private Board board;
  
 @Override
 protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
   HttpServletResponse arg1) throws Exception {
  // TODO Auto-generated method stub
  
  System.out.println("<<===== START =====>>");
  System.out.println("Board Name-1 : " + board.getBoardName());
  System.out.println("Board Check-1 : " + board.checkBoard());
  System.out.println("<<===== END =====>>");
  
  return null;
 }

}

 

spring302-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<context:annotation-config/>
<aop:aspectj-autoproxy/>
<context:component-scan base-package="sp.aop.service"/>


<!-- HandlerMapping 설정 -->
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/aoptest01.sp">boardController</prop>
</props>
</property>
</bean>


<!-- 각종 bean 설정 -->
<bean id="boardController" class="sp.aop.controller.BoardController"></bean>
<bean id="testAspect" class="sp.aop.aspect.TestAspect"></bean>

</beans>

● <aop:aspectj-autoproxy/> 태그를 사용하는데 이 태그를 사용하게 되면 @Aspect 어노테이션이 적용된 빈 객체를 Aspect로 사용하게 됨

 

 

TestAspect.java - 1

package sp.aop.aspect;

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

 

@Aspect
public class TestAspect {

 

 @Before("execution(public * sp.aop.service.*.get*(..))")
 public void before(JoinPoint joinPoint){
  System.out.println("<<< before >>>");
 }
}

 

******************************************************************************************

● @Before 어노테이션의 값으로는 AspectJ의 Pointcut 표현식이나 @Pointcut 어노테이션이 적용된 메서드 이름이 올 수 있음. (위 예제의 경우는 AspectJ의 표현식을 사용)

● JoinPoint 타입의 파라미터를 첫 번째 파라미터로 가질 수 있음

 

<<===== START =====>>
<<< before >>>
Board Name-1 : Notice Board
Board Check-1 : Y
<<===== END =====>>

 

 

TestAspect.java - 2

package sp.aop.aspect;

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

 

@Aspect
public class TestAspect {
 
 @AfterReturning("execution(public * sp.aop.service.*.*(..))")
 public void afterReturning(JoinPoint joinPoint){
  System.out.println("<<< after returning >>>");
 }
}

<<===== START =====>>
<<< after returning >>>
Board Name-1 : Notice Board
<<< after returning >>>
Board Check-1 : Y
<<===== END =====>>

 

 

TestAspect.java - 3

package sp.aop.aspect;

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

 

@Aspect
public class TestAspect {
 

 @AfterReturning(pointcut="execution(public * sp.aop.service..*(..))", returning="ret")
 public void afterReturning(JoinPoint joinPoint, Object ret){
  System.out.println("<<< after returning >>>");
  System.out.println("<<< after returning return : " + ret.toString());
 }
} 

 

******************************************************************************************

● Advice 구현 메서드 내에서 Advice 대상 객체가 리턴한 값을 사용하고 싶다면 returning 속성을 이용해서 리턴 값을 전달받을 파라미터의 이름을 지정

● 특정타입의 리턴 객체에 대해 메서드를 실행하고 싶다면 다음과 같이 한정하고 싶은 타입의 파라미터를 사용

  => public void afterReturning(JoinPoint joinPoint, AAA ret){ ... }

 

<<===== START =====>>
<<< after returning >>>
<<< after returning return : Notice Board
Board Name-1 : Notice Board
<<< after returning >>>
<<< after returning return : Y
Board Check-1 : Y
<<===== END =====>>

 

 

 

 

[참고자료] Spring 3.0 프로그래밍-최범균

+ Recent posts