티스토리 뷰

spring

day2 class04 JoinPoint와 바인드 변수

일상다반ㅅㅏ 2019. 1. 6. 17:17

횡단 관심에 해당하는 어드바이스 메소드를 의미 있게 구현하려면 클라이언트가 호출한 비즈니스 메소드의 정보가 필요하다.


1. Before 어드바이스

- 호출된 메소드 시그니처만 알 수 있으면 다양한 사전 처리 로직을 구현할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
// BeforeAdvice.java
 
import org.aspectj.lang.JoinPoint;
 
public class BeforeAdvice {
 
    public void beforeLog(JoinPoint jp) {
        String method = jp.getSignature().getName();
        Object[] args = jp.getArgs();
        
        System.out.println("[사전 처리] " + method + "() 메소드 ARGS 정보 : " + args[0].toString());
    }
}
cs


2. After Returning 어드바이스

- 어떤 메소드가 어떤 값을 리턴했는지 알아야 사후 처리 기능을 다양하게 구현할 수 있다.

- returnObj를 '바인드 변수'라고 한다.

- 바인드 변수는 비즈니스 메소드가 리턴한 결과값을 바인딩할 목적으로 사용하며, 어떤 값이 리턴될지 모르기 때문에 Object 타입으로 선언한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
// AfterReturningAdvice.java
 
public void afterLog(JoinPoint jp, Object returnObj) {
    String method = jp.getSignature().getName();
    if(returnObj instanceof UserVO) {
        UserVO user = (UserVO) returnObj;
        if(user.getRole().equals("Admin")) {
            System.out.println(user.getName() + " 로그인(Admin)");
        }
    }
    
    System.out.println("[사후 처리] " + method + "() 메소드 리턴값 : " + returnObj.toString());
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- applicationContext.xml -->
 
<bean id="before" class="com.springbook.biz.common.BeforeAdvice"></bean>
<bean id="afterReturning" class="com.springbook.biz.common.AfterReturningAdvice"></bean>
<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="after">
        <aop:after-returning pointcut-ref="getPointcut" method="afterLog" returning="returnObj"/>
    </aop:aspect>
</aop:config>
cs


3. After Trhowing 어드바이스

- 어떤 메소드에서 어떤 예외가 발생했는지 알 수 있으면 발생한 예외의 종류에 따라 예외 처리를 구현할 수 있다.

- 바인드 변수는 비즈니스 메소드에서 발생한 예외 객체를 바인딩하며, 모든 예외 객체를 바인드 할 수 있도록 예외 클래스의 최상위 타입인 Exception으로 선언한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// AfterThrowingAdvice.java
 
public void exceptionLog(JoinPoint jp, Exception exceptObj) {
    String method = jp.getSignature().getName();
    System.out.println(method + "() 비즈니스 로직 수행 중 예외 발생!");
    
    if(exceptObj instanceof IllegalArgumentException) {
        System.out.println("부적합한 값이 입력되었습니다.");
    } else if(exceptObj instanceof NumberFormatException) {
        System.out.println("숫자 형식의 값이 아닙니다.");
    } else if(exceptObj instanceof Exception) {
        System.out.println("문제가 발생했습니다.");
    } 
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- applicationContext.xml -->
 
<bean id="before" class="com.springbook.biz.common.BeforeAdvice"></bean>
<bean id="afterReturning" class="com.springbook.biz.common.AfterReturningAdvice"></bean>
<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="allPointcut" method="exceptionLog" throwing="exceptObj"/>
    </aop:aspect>
</aop:config>
cs


4. Around 어드바이스

- 다른 어드바이스와는 다르게 반드시 ProceedingJoinPoint 객체를 매개변수로 받아야 한다. (proceed를 사용하기 위해)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// AroundAdvice.java
 
public Object aroundLog(ProceedingJoinPoint pjpthrows Throwable {
    String method = pjp.getSignature().getName();
    
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
        
    Object returnObj = pjp.proceed();
        
    stopWatch.stop();
    System.out.println(method + "() 메소드 수행에 걸린 시간 : " + stopWatch.getTotalTimeMillis() + "(ms)초");
        
    return returnObj;
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- applicationContext.xml -->
 
<bean id="before" class="com.springbook.biz.common.BeforeAdvice"></bean>
<bean id="afterReturning" class="com.springbook.biz.common.AfterReturningAdvice"></bean>
<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="around">
        <aop:around pointcut-ref="allPointcut" method="aroundLog"/>
    </aop:aspect>
</aop:config>
cs


'spring' 카테고리의 다른 글

day2 class06 스프링 JDBC  (0) 2019.01.13
day2 class05 어노테이션 기반 AOP  (0) 2019.01.06
day2 class03 어드바이스 동작 시점  (0) 2018.12.29
day2 class02 AOP용어 및 기본 설정  (0) 2018.12.28
day2 class01 스프링AOP  (0) 2018.12.26