@Aspect 어노테이션을 이용해서 AOP를 구현하는 과정은 XML스키마 기반의 AOP를 구현하는 과정과 거의 유사하며, 차이점은 다음과 같다.

  ① @Aspect 어노테이션을 이용해서 Aspect 클래스를 구현한다. 이때 Aspect 클래스는 Advice를 구현한 메서드와 Pointcut을 포함한다.

  ② XML 설정에서 <aop:aspectj-autoproxy/>를 설정한다.

 

 

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

}

 

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;

 

@Aspect
public class LogAspect {

 

 @Pointcut("execution(public * sp.aop.service.*.get*(..))")
 private void profileTarget(){};
 
 @Pointcut("execution(public * sp.aop.service.*.check*(..))")
 private void checkTarget(){};
 
 
 @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("****");
  }
 }
 
 @Around("checkTarget()")
 public Object allCheck(ProceedingJoinPoint joinPoint) throws Throwable{
  System.out.println("** check start **");
  
  try{
   Object ret = joinPoint.proceed();
   return ret;
  }finally{
   System.out.println("** check end **");
  }
 }
 
}

●@Pointcut 어노테이션은 Pointcut을 정의하는 AspectJ 표현식을 값으로 갖게 됨.

@Pointcut 어노테이션을 적용한 메서드는 리턴 타입이 void 여야 함

●@Pointcut 어노테이션을 이용해서 Pointcut을 정의하면, Advice 관련 어노테이션에서 해당 메서드 이름을 이용해서 Pointcut을 사용할 수 있게 됨. 

 

 

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

</beans>

<aop:aspectj-autoproxy/> 태그를 사용하게 되면 @Aspect 어노테이션이 적용된 빈 객체를 Aspect로 사용하게 됨. 

 

 

결과

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

 

 

 

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

 

+ Recent posts