<aop:pointcut> 태그와 각 Advice 설정 태그의 pointcut-ref 속성을 이용해서 Pointcut을 설정하고 여러 Advice에서 동일한 Pointcut을 참조

 

 

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("");
  System.out.println("Board Check-1 : " + board.checkBoard());
  System.out.println("<<===== END =====>>");
  
  return null;
 }

}

 

LogAdvice.java

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(JoinPoint joinPoint){
  System.out.println("***** before *****");
 }
 
 public void afterReturning(Object obj){
  System.out.println("***** after-returning *****");
  System.out.println("obj : " + obj.toString());
 }
 
 public void afterThrowing(Throwable exp){
  System.out.println("***** after-throwing *****");
  System.out.println("ex : " + exp.toString());
 }
 
 public void afterFinally(JoinPoint joinPoint){
  System.out.println("***** after finally *****");
  System.out.println("joinPoint : " + joinPoint.toString());
 }
 
}

 

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>
 
 <!-- Advice 클래스를 빈으로 등록 -->
 <bean id="logAdvice" class="sp.aop.advice.LogAdvice"></bean>
 
 <aop:config>
  <aop:aspect id="loggingAspect" ref="logAdvice">
   <aop:pointcut id="getLog" expression="execution(public * sp.aop.service.*.get*())"/>
   <aop:pointcut id="checkLog" expression="execution(public * sp.aop.service.*.check*())"/>
   
   <aop:after pointcut-ref="getLog" method="before"/>
   <aop:after-returning pointcut-ref="getLog" method="afterReturning" returning="obj"/>

   <aop:after pointcut-ref="checkLog" method="afterFinally"/>
   <aop:around pointcut-ref="checkLog" method="logPrint"/>
  </aop:aspect>
 </aop:config>

</beans>

 

결과

<<===== START =====>>
***** before *****
***** after-returning *****
obj : Notice Board
Board Name-1 : Notice Board

***** START *****
*
**
***** after finally *****
joinPoint : execution(String sp.aop.service.Board.checkBoard())
***
****
***** END *****
Board Check-1 : Y
<<===== END =====>>

 

 

 

 

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

 

+ Recent posts