티스토리 뷰

spring

day2 class03 어드바이스 동작 시점

일상다반ㅅㅏ 2018. 12. 29. 17:32

어드바이스는 각 조인포인트에 삽입되어 동작할 횡단 관심에 해당하는 공통 기능이다.


1. before 어드바이스

- 비즈니스 메소드 실행 전 동작한다.

1
2
3
4
5
6
7
8
9
10
11
12
<!-- applicationContext.xml -->
 
<bean id="before" class="com.springbook.biz.common.BeforeAdvice"></bean>
 
<aop:config>
    <aop:pointcut id="allPointcut" expression="execution(* com.springbook.biz..*Impl.*(..))"/>
    <aop:pointcut id="getPointcut" expression="execution(* com.springbook.biz..*Impl.get*(..))"/>
    
    <aop:aspect ref="before">
        <aop:before pointcut-ref="allPointcut" method="beforeLog"/>
    </aop:aspect>
</aop:config>
cs


2. after returning 어드바이스

- 비즈니스 메소드가 성공적으로 리턴되면 동작한다.

1
2
3
4
5
6
7
8
9
10
11
12
<!-- applicationContext.xml -->
 
<bean id="afterReturning" class="com.springbook.biz.common.AfterReturningAdvice"></bean>
    
<aop:config>
    <aop:pointcut id="allPointcut" expression="execution(* com.springbook.biz..*Impl.*(..))"/>
    <aop:pointcut id="getPointcut" expression="execution(* com.springbook.biz..*Impl.get*(..))"/>
    
    <aop:aspect ref="afterReturning">
        <aop:after-returning pointcut-ref="getPointcut" method="afterLog"/>
    </aop:aspect>
</aop:config>
cs


3. after throwing 어드바이스

- 비즈니스 메소드 실행 중 예외가 발생하면 동작한다. (try~catch 블록에서 catch 블록에 해당)

1
2
3
4
5
6
7
8
9
10
11
12
<!-- applicationContext.xml -->
 
<bean id="afterThrowing" class="com.springbook.biz.common.AfterThrowingAdvice"></bean>
    
<aop:config>
    <aop:pointcut id="allPointcut" expression="execution(* com.springbook.biz..*Impl.*(..))"/>
    <aop:pointcut id="getPointcut" expression="execution(* com.springbook.biz..*Impl.get*(..))"/>
    
    <aop:aspect ref="afterThrowing">
        <aop:after-throwing pointcut-ref="getPointcut" method="exceptionLog"/>
    </aop:aspect>
</aop:config>
cs

- 강제로 예외사항을 발생시켜 준다.

1
2
3
4
5
6
7
8
// BoardServiceImpl.java
 
public void insertBoard(BoardVO vo) {
    if(vo.getSeq() == 0) {
        throw new IllegalArgumentException("0번 글은 등록할 수 없습니다.");
    }
    boardDAO.insertBoard(vo);
}
cs


4. after 어드바이스

- 비즈니스 메소드가 실행된 후, 무조건 실행한다. (try~catch 블록에서 finally 블록에 해당)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!-- applicationContext.xml -->
 
<bean id="afterThrowing" class="com.springbook.biz.common.AfterThrowingAdvice"></bean>
<bean id="after" class="com.springbook.biz.common.AfterAdvice"></bean>
<bean id="around" class="com.springbook.biz.common.AroundAdvice"></bean>
    
<aop:config>
    <aop:pointcut id="allPointcut" expression="execution(* com.springbook.biz..*Impl.*(..))"/>
    <aop:pointcut id="getPointcut" expression="execution(* com.springbook.biz..*Impl.get*(..))"/>
    
    <aop:aspect ref="afterThrowing">
        <aop:after-throwing pointcut-ref="getPointcut" method="exceptionLog"/>
    </aop:aspect>
 
    <aop:aspect ref="after">
        <aop:after pointcut-ref="allPointcut" method="finallyLog"/>
    </aop:aspect>
</aop:config>
cs


5. around 어드바이스

- 메소드 호출 자체를 가로채 비즈니스 메소드 실행 전후에 처리할 로직을 삽입한다.

1
2
3
4
5
6
7
8
9
10
11
<!-- applicationContext.xml -->
<bean id="around" class="com.springbook.biz.common.AroundAdvice"></bean>
    
<aop:config>
    <aop:pointcut id="allPointcut" expression="execution(* com.springbook.biz..*Impl.*(..))"/>
    <aop:pointcut id="getPointcut" expression="execution(* com.springbook.biz..*Impl.get*(..))"/>
    
    <aop:aspect ref="around">
        <aop:around pointcut-ref="allPointcut" method="aroundLog"/>
    </aop:aspect>
</aop:config>
cs


1
2
3
4
5
6
7
8
9
10
11
12
// AroundAdvice.java
 
public class AroundAdvice {
 
    public Object aroundLog(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("[before] : 비즈니스 메소드 수행 전에 처리할 내용...");
        Object returnObj = pjp.proceed();
        System.out.println("[after] : 비즈니스 메소드 수행 후에 처리할 내용...");
        
        return returnObj;
    }
}
cs