※ 하나의 JoinPoint에 한 개 이상의 Advice가 적용될 경우, 순서를 명시적으로 지정할 수 있음

  지정방법

    ▶ org.springframework.core.annotation.Order 어노테이션을 적용

    org.springframework.core.Ordered 인터페이스를 구현

 

 

 

■ @Order 어노테이션을 이용하는 방법

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("<<===== END =====>>");
  
  return null;
 }

}

 

TestAspect.java

package sp.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;


@Aspect
@Order(1)

public class TestAspect {

 

 @Pointcut("execution(* sp.aop.service.*.get*())")
 private void getInfo(){};
  
 @Before("getInfo()")
 public void beforeLog(JoinPoint joinPoint){
  System.out.println("--- beforeLog ---");
 }
}

 

LogAspect.java

package sp.aop.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;

 

@Aspect
@Order(2)

public class LogAspect {

 

 @Pointcut("execution(public * sp.aop.service.*.get*(..))")
 private void profileTarget(){};
  
 @Around("profileTarget()")
 public Object allLog(ProceedingJoinPoint joinPoint) throws Throwable{
  System.out.println("*");
  System.out.println("**");
  
  try{
   Object ret = joinPoint.proceed();
   return ret;
  }finally{
   System.out.println("***");
   System.out.println("****");
  }
 }
}

 

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>
 <bean id="logAspect" class="sp.aop.aspect.LogAspect"></bean>

</beans>

 

 

결과 => LogAspect : Order(2), TestAspect : Order(1)

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

 

결과 => LogAspect : Order(1), TestAspect : Order(2)

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

● 적용 순서 값이 낮은 Advice의 우선 순위가 더 높다. 순서 값이 1인 Advice가 순서 값인 2인 Advice보다 우선 순위가 높다. 메서드 호출 전, 메서드 호출에 대해서느 우선 순위가 높은 Advice가 먼저 실행되고, 메서드 호출 후에 대해서는 우선 순위가 낮은 Advice가 먼저 실행

 

 

 

■ XML 스키마를 사용할 경우 <aop:asect> 태그의 order 속성을 사용

 <aop:config>
  <aop:aspect id="" ref="" order="1">
   <aop:pointcut expression="" id=""/>
   <aop:around method=""/>
  </aop:aspect>
 </aop:config>

 

 

 

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

'프로그램 > Spring 3.0' 카테고리의 다른 글

스프링 MVC의 처리 흐름 - 간단 예제  (0) 2012.05.13
스프링 MVC 주요 구성 요소  (2) 2012.05.13
Pointcut의 조합  (0) 2012.05.12
AspectJ의 Pointcut 표현식  (0) 2012.05.12
JoinPoint의 사용  (0) 2012.05.11

+ Recent posts