※ @After : After Advice를 구현

  - 대상 객체의 메서드를 실행하는 도중에 예외가 발생했는지의 여부와 상관없이 메서드 실행 후 공통 기능을 실행(try~catch~finally의 finally 블록과 비슷) 

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

 

@Around : Around 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";
 }

 @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>

 

 

TestAspect.java - After Advice

package sp.aop.aspect;

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


@Aspect
public class TestAspect {


 @After("execution(public * sp.aop.service.*.*())")
 public void afterFinally(JoinPoint joinPoint){
  System.out.println("<<< after >>>");
 }
}

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

 

 

TestAspect.java - Around Advice

package sp.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;


@Aspect
public class TestAspect {


 @Around("execution(public * sp.aop.service.*.*(..))")
 public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
  System.out.println("*");
  System.out.println("**");
  
  Object obj = joinPoint.proceed();
  
  System.out.println("***");
  System.out.println("****");
  
  return obj;
 }
}

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

 

 

 

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

+ Recent posts