스프링 AOP: JoinPoint
1. JoinPoint 5가지
1. Before
2. After
3. AfterReturning
4. AfterThrowing
5. Around
2. 사용법 3가지
1. 애너테이션Annotation(요즘 것)
2. XML(예전 것)
3. 포조(잘 사용하지 않는다)
3-1. 호출 순서(오류 없을 때)
Before
Around
After
AfterReturning
3-2. 호출 순서(오류 났을 때)
Before
Around는 실행되지 않음
After
(AfterReturning)
AfterThrowing
참고)
Before는 메소드 실행 이전에 반드시 실행한다. ex) 오토커밋을 false로 바꾼다.
AfterReturning은 오류가 났을 때만 실행된다. 따라서 트랜잭션의 롤백용으로 자주 사용한다. ex) 롤백
After는 에러가 나든 안나든 꼭 수행된다. 따라서 오토커밋 설정용으로 자주 사용한다. ex) 오토커밋을 true로 돌려 놓는다.
4. pointcut
크게 execution과 within 이 있다.
4-1. within: 패키지로 적용
1) 특정 패키지의 모든 메소드에 대해서 적용: within(패키지.*)
ex) @AfterThrowing(pointcut=”within(com.sist.dao.*)”)
2) 특정 패키지 및 하위 패키지까지 모두 적용 within(패키지..*)
ex) @AfterThrowing(pointcut=”within(com.sist.dao..*)”)
4-2. execution: 메소드로 적용
@Before(“execution(* com.sist.aop2.MyDAO.tx*(..))”)
이것은
@Before(value=”execution(* com.sist.aop2.MyDAO.tx*(..))”) 와 동일하다. (After와 Before는 매개변수 value를 씀)
“execution(리턴형 클래스명.메소드명(매개변수))” 으로 쓰면된다. 여기서 매개변수가 (*)이면 매개변수 1개, (*,*)는 매개변수 2개, (*,*,*)는 매개변수 3개, (..)는 0개 이상, (String,..)은 String 한 개가 있고 이후에는 상관없음을 의미한다.
참고)
@After(“execution(* com.sist.aop2.MyDAO.tx*(..))”)
@Before(“execution(* com.sist.aop3.EmpDAO.*(..))”)
@After(“execution(* com.sist.aop3.EmpDAO.*(..))”)
5. 에러 처리
에러가 났을 때 가져올 객체이름이 ex 라면 throwing=”ex”라고 써주어야 한다.
@AfterThrowing(pointcut=”execution(* com.sist.dao.BoardImpl.get*())||execution(* com.sist.dao.BoardImpl.check*(..))”,throwing=”ex”)
public void afterThrowing(JoinPoint p,Throwable ex){
System.out.println(“==AfterThrowing 수행==”);
//어떤 메소드에서 실행되었느냐
System.out.println(“Method Name:”+p.getSignature().getName());
//무슨 에러냐
System.out.println(“Exception:”+ex.getMessage());
}
6. 자바 소스
@Aspect
public class TestAspect2 {
@Before(“within(com.sist.dao.*)”)
public void aaa(JoinPoint p){
System.out.println(“==Before==”);
}
@After(“within(com.sist.dao.*)”)
public void bbb(JoinPoint p){
System.out.println(“==After==”);
}
@Around(“within(com.sist.dao.*)”)
public Object ccc(ProceedingJoinPoint p) throws Throwable{
Object obj=null;
try{
obj=p.proceed();//getBoardName()
System.out.println(“==Around==”);
}catch(Exception ex){System.out.println(ex.getMessage());}
return obj;
}
@AfterReturning(pointcut=”within(com.sist.dao.*)”,returning=”ret”)
public void ddd(JoinPoint p,Object ret){
System.out.println(“==AfterReturning==”);
System.out.println(“return Value:”+ret.toString());
}
@AfterThrowing(pointcut=”within(com.sist.dao.*)”,throwing=”ex”)
public void eee(JoinPoint p,Throwable ex){
System.out.println(“==AfterThrowing==”);
}
}
7. XML로 만들 때
<bean id=”testAspect” class=”com.sist.aspect.TestAspect”/>
<aop:config>
<aop:aspect ref=”testAspect”>
<aop:after method=”bbb” pointcut=”within(com.sist.dao.*)”/>
<aop:after-returning method=”ddd” pointcut=”within(com.sist.dao.*)” returning=”ret”/>
<aop:after-throwing method=”eee” pointcut=”within(com.sist.dao.*)” throwing=”ex”/>
<aop:before method=”aaa” pointcut=”within(com.sist.dao.*)”/>
<aop:around method=”ccc” pointcut=”within(com.sist.dao.*)”/>
</aop:aspect>
</aop:config>