※ 각각의 표현식은 '&&'나 '||' 연산자를 이용하여 연결할 수 있음
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
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 프로그래밍-최범균