※ 각각의 표현식은 '&&'나 '||' 연산자를 이용하여 연결할 수 있음

 

 

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
  int num = 100 / 0;
  return "Notice Board";
 }

 

 @Override
 public String checkBoard() {
  // TODO Auto-generated method stub
  int num = 100 / 0;
  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;
 }

}

 

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

package sp.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


@Aspect
public class TestAspect {


 @AfterThrowing(pointcut="execution(* sp.aop.service.*.get*()) || execution(* sp.aop.service.*.check*(..))", throwing="ex")
 public void afterThrowing(JoinPoint joinPoint, Throwable ex){
  System.out.println("--- after throwing ---");
  System.out.println("name : " + joinPoint.getSignature().getName());
  System.out.println("throwable message : " + ex.getMessage());
 }
}

<<===== START =====>>
--- after throwing ---
name : getBoardName
throwable message : / by zero
2012. 5. 12 오후 4:05:27 org.apache.catalina......

 

 

TestAspect.java - 2

package sp.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


@Aspect
public class TestAspect {

 

 @Pointcut("execution(* sp.aop.service.*.get*())")
 private void getInfo(){};
 
 @Pointcut("execution(* sp.aop.service.*.check*(..))")
 private void checkInfo(){};
 
 
 @AfterThrowing(pointcut="getInfo() || checkInfo()", throwing="ex")
 public void afterThrowing(JoinPoint joinPoint, Throwable ex){
  System.out.println("--- after throwing ---");
  System.out.println("name : " + joinPoint.getSignature().getName());
  System.out.println("throwable message : " + ex.getMessage());
 }
}

<<===== START =====>>
--- after throwing ---
name : getBoardName
throwable message : / by zero
2012. 5. 12 오후 4:17:36 org.apache.catalina......

 

 

TestAspect.java - 3

package sp.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


@Aspect
public class TestAspect {

 

 @Pointcut("execution(* sp.aop.service.*.get*())")
 private void getInfo(){};
 
 @Pointcut("execution(* sp.aop.service.*.check*(..))")
 private void checkInfo(){};
 
 @Pointcut("getInfo() || checkInfo()")
 private void allInfo(){};
 
 @AfterThrowing(pointcut="allInfo()", throwing="ex")
 public void afterThrowing(JoinPoint joinPoint, Throwable ex){
  System.out.println("--- after throwing ---");
  System.out.println("name : " + joinPoint.getSignature().getName());
  System.out.println("throwable message : " + ex.getMessage());
 }
}

<<===== START =====>>
--- after throwing ---
name : getBoardName
throwable message : / by zero
2012. 5. 12 오후 4:21:12 org.apache.catalina......

 

 

■ XML 스키마를 이용하여 Aspect를 설정하는 경우에도 '&&'(and)나 '||'(or) 연산자를 사용

 <aop:config>
  <aop:aspect id="test" ref="testAspect">
   <aop:pointcut expression="execution(* sp.aop.service.*.get*()) and execution(* sp.aop.service.*.check*(..))" id="publicPointcut"/>
   <aop:before method=""/>
  </aop:aspect>
 </aop:config>

 

 

 

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

+ Recent posts