1. DispatcherServlet - Front Controller 2. Controller - 클라이언트 요청 처리를 수행하는 Controller 3. HandlerMapping - 클라이언트의 요청을 처리할 Controller를 찾는 작업처리 4. View - 응답하는 로직을 처리 5. ViewReslover - 응답할 View를 찾는 작업을 처리 6. ModelAndView - 응답할 View와 View에게 전달할 값을 저장하는 용도의 객체
1. @Aspect 어노테이션을 이용하여 Aspect 클래스에 직접 Advice 및 Point등을 직접 설정 2. 설정파일에 <aop:aspectj-autoproxy/> 를 추가 해야함 3. Aspect class를 <bean>으로 등록 4. 어노테이션(Annotation) - @Aspect : Aspect 클래스 선언 - @Before("pointcut") - @AfterReturning(pointcut="", returning="") - @AfterThrowing(pointcut="",throwing="") - @After("pointcut") - @Around("pointcut")
5. Around를 제외한 나머지 메소드들은 첫 argument로 JoinPoint를 가질 수 있다. 6. Around 메소드는 argument로 ProceedingJoinPoint를 가질 수 있다.
@Aspect public class AdviceClass { @Before("within(aop.core.CoreClass)")//pointcut 지정 public void beforLogger(){ System.out.println("AdviceClass.before.beforeLogger()---------------------"); } @AfterThrowing(pointcut="within(aop.core.CoreClass)",throwing="ex") public void afterThrowLogger(Throwable ex){ System.out.println("AdviceClass.afterThrowLogger()----------------------"); System.out.println(ex.getMessage()); } @AfterReturning(pointcut="within(aop.core.CoreClass)",returning="ret") public void afterReturnLogger(Object ret){ System.out.println("AdviceClass.afterReturnLogger()--------------"); System.out.println(ret); } @After("within(aop.core.CoreClass)")//pointcut만 적용할 경우 pointcut=을 생략하고 within 정의 public void afterLogger(){ System.out.println("AdviceClass.afterLogger()----------------------"); } @Around("within(aop.core.CoreClass)") public Object aroundLogger(ProceedingJoinPoint jp)throws Throwable{ System.out.println("AdviceClass.aroundLogger() 실행----------------"); return jp.proceed(); } }
public class CoreClass { public void businessMethod(int i){ if(i<0){ throw new RuntimeException("i가 0보다 작다"); } System.out.println("Business메소드가 실행되었습니다."); } }
* 결과
------------정상처리---------------- AdviceClass.before.beforeLogger()--------------------- AdviceClass.aroundLogger() 실행---------------- Business메소드가 실행되었습니다. AdviceClass.afterReturnLogger()-------------- null AdviceClass.afterLogger()---------------------- ------------에러내기---------------- AdviceClass.before.beforeLogger()--------------------- AdviceClass.aroundLogger() 실행---------------- AdviceClass.afterThrowLogger()---------------------- i가 0보다 작다 Exception in thread "main" AdviceClass.afterLogger()---------------------- java.lang.RuntimeException: i가 0보다 작다 at aop.core.CoreClass.businessMethod(CoreClass.java:6) at aop.core.CoreClass$$FastClassByCGLIB$$359776d6.invoke(<generated>) at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191) at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:692) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:42) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80) at aop.common.AdviceClass.aroundLogger(AdviceClass.java:34) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625) at aop.core.CoreClass$$EnhancerByCGLIB$$cebdb4e6.businessMethod(<generated>) at TestMain.main(TestMain.java:14)