(1) Before Advice : <aop:before>

(2) After Returning Advice : <aop:after-returning>

 

 

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

}

 

 

(1) Before Advice : <aop:before>

  - 대상 객체 및 호출되는 메서드에 대한 정보나 전달되는 파라미터에 대한 정보가 필요한 경우 org.aspectj.lang.JoinPoint 타입의 파라미터를 메서드에 전달함 => public void before(JoinPoint joinPoint){ ... }

  - 리턴 타입이 대부분 void인데, 그 이유는 리턴값을 갖더라도 실제 Advice의 적용 과정에 아무런 영향이 없기 때문.

  - 주의사항 : 메서드에서 예외를 발생시킬 경우 대상 객체의 메서드가 호출되지 않게 됨

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 before(){
 public void before(JoinPoint joinPoint){
  System.out.println("***** before *****");
 }

}

<?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:before pointcut-ref="logPrint" method="before"/>
  </aop:aspect>
 </aop:config>

 

</beans>

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

 

 

(2) After Returning Advice : <aop:after-returning>

  - 대상 객체의 메서드가 정상적으로 실행된 후에 공통 기능을 적용하고 싶을 때 사용하는 Advice

  - Advice를 구현한 메서드에서 리턴 값을 사용하고 싶다면 returning 속성을 사용하여 리턴 값을 전달받을 파라미터의 이름을 명시

    <aop:after-returning pointcut-ref="publicMethod" method="afterReturning" returning="obj"/>

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

    public void afterReturning(JoinPoint joinPoint, Object obj){ ... }

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 afterReturning(Object obj){
  System.out.println("***** after-returning *****");
  System.out.println("obj : " + obj.toString());
 }

} 

<?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-returning pointcut-ref="logPrint" method="afterReturning" returning="obj"/>
  </aop:aspect>
 </aop:config>

 

</beans>

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

 

 

 

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

+ Recent posts