<aop:after>

  - After Advice는 대상 객체의 메서드가 정상적으로 실행되었는지 아니면 예오를 발생 시켰는지의 여부에 상관없이 메서드 실행이 종료된 이후에 적용되는 Advice로서 try~catch~finally 블록에서 finally 블록과 비슷한 기능을 수행

 

<aop:around>

  - Around Advice는 Before, After Returning, After Throwing, After Advice를 모두 구현할 수 있는 Advice

  - Around Advice를 구현한 메서더는 org.aspectj.lang.ProceedingJoinPoint를 반드시 첫 번째 파라미터로 지정해야 함

 

 

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";
 }
}

 

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";
 }
}

 

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("<<===== RUNNING =====>>");
  System.out.println("Board Name-2 : " + board.getBoardName());
  System.out.println("<<===== END =====>>");
  
  return null;
 }

}

 

 

package sp.aop.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

 

public class LogAdvice {

 public Object logPrint(ProceedingJoinPoint joinPoint) throws Throwable{
  System.out.println("***** START *****");
  try{
   System.out.println("*");
   System.out.println("**");
   
   Object ret = joinPoint.proceed();
   
   System.out.println("***");
   System.out.println("****");
   
   return ret;
  }finally{
   System.out.println("***** END *****");
  }
 }
 
 public void afterFinally(JoinPoint joinPoint){
  System.out.println("***** after finally *****");
  System.out.println("joinPoint : " + joinPoint.toString());
 }

}

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

메서드에는 파라미터를 갖지 않음

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

 

<?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/>
 <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>
 
 <!-- Advice 클래스를 빈으로 등록 -->
 <bean id="timeCheckAdvice" class="sp.aop.advice.TimeCheckAdvice"></bean>
 <bean id="logAspect" class="sp.aop.advice.LogAdvice"></bean>
 
 <!-- Aspect 설정 : Advice를 어떤 PointCut에 적용할 지 설정 -->
 <aop:config>
  <aop:aspect id="logPrtAspect" ref="logAspect">
   <aop:pointcut id="logPrint" expression="execution(* sp.aop..*.*(..))"/>
   <aop:after pointcut-ref="logPrint" method="afterFinally" />
  </aop:aspect>
 </aop:config>

 

</beans>

<<===== START =====>>
***** after finally *****
joinPoint : execution(String sp.aop.service.Board.getBoardName())
Board Name-1 : Notice Board
<<===== RUNNING =====>>
***** after finally *****
joinPoint : execution(String sp.aop.service.Board.getBoardName())
Board Name-2 : Notice Board
<<===== END =====>>

 

 

package sp.aop.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

 

public class LogAdvice {

 public Object logPrint(ProceedingJoinPoint joinPoint) throws Throwable{
  System.out.println("***** START *****");
  try{
   System.out.println("*");
   System.out.println("**");
   
   Object ret = joinPoint.proceed();
   
   System.out.println("***");
   System.out.println("****");
   
   return ret;
  }finally{
   System.out.println("***** END *****");
  }
 }

 
 public void afterFinally(JoinPoint joinPoint){
  System.out.println("***** after finally *****");
  System.out.println("joinPoint : " + joinPoint.toString());
 }
}

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

●Around Advice를 구현한 메서드는 org.aspectj.lang.ProceedingJoinPoint를 반디스 첫 번째 파라미터로 지정해야 함 - 그렇지 않을 경우 예외 발생

●ProceedingJoinPoint의 proceed() 메서드를 호출하면 프록시 대상 객체의 실제 메서드를 호출하게 된다. 따라서 ProceedingJoinPoint.proceed() 메서드를 호출하기 전과 후에 필요한 작업을 수행할 수 있다.

 

<?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/>
 <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>
 
 <!-- Advice 클래스를 빈으로 등록 -->
 <bean id="timeCheckAdvice" class="sp.aop.advice.TimeCheckAdvice"></bean>
 <bean id="logAspect" class="sp.aop.advice.LogAdvice"></bean>
 
 <!-- Aspect 설정 : Advice를 어떤 PointCut에 적용할 지 설정 -->
 <aop:config>
  <aop:aspect id="logPrtAspect" ref="logAspect">
   <aop:pointcut id="logPrint" expression="execution(* sp.aop..*.*(..))"/>
   <aop:around pointcut-ref="logPrint" method="logPrint" />
  </aop:aspect>
 </aop:config>

 

</beans>

<<===== START =====>>
***** START *****
*
**
***
****
***** END *****
Board Name-1 : Notice Board
<<===== RUNNING =====>>
***** START *****
*
**
***
****
***** END *****
Board Name-2 : Notice Board
<<===== END =====>>

 

 

 

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

 

+ Recent posts