네트워크

AOP - Advice 종류

GaeGim 2022. 10. 27. 20:01
반응형
  • 종류

 

<aop:before>

- Joinpoint가 실행되기 전에 실행되는 advice.

  예외를 던지지 않는 이상 Joinpoint의 실행을 막을 수 없다.

 

 

 

<aop:after-returning>

- 정상적으로 Joinpoint 실행 후 실행되는 advice.

 

 

 

<aop:after-throwing>

- Joinpoint 실행 중 exception 발생 시 실행되는 advice.

 

 

 

<aop:after>

- 메서드 실행 중 exception이 발생하든 안하든 실행되는 advice.

 

 

 

<aop:around>

- 메서드 실행 전/후, exception 발생 시 advice 실행.

  Joinpoint를 실행할 지 혹은 자체적인 값을 리턴하거나 예외를 던져 생략하든지 등을 결정할 수 있음.

 

 

 

 

 

 

 

 

  • 적용 예시
logAop.java
@Aspect
public class logAop {
    private void pointcutMethod() {

    }

    @Around("pointcutMethod()")
    public Object LoggerAop(ProceedingJoinPoint joinPoint) throws Throwable {
        String signatureStr = joinPoint.getSignature().toShortString();
        System.out.println(signatureStr + " is start");
        long st = System.currentTimeMillis();

        try {
            Object obj = joinPoint.proceed();
            return obj;
        } finally {
            long et = System.currentTimeMillis();
            System.out.println(signatureStr + " is finished");
            System.out.println(signatureStr + "경과 시간 : " + (et - st));
        }
    }

    @Before("pointcutMethod()")
    public void beforeAdvice() {
        System.out.println("beforeAdvice()");
    }

    public void beforeAdvice(JoinPoint joinPoint) {
        System.out.println("beforeAdvice()");
    }
    public void afterAdvice() {
        System.out.println("afterAdvice()");
    }
    public void afterThrowingAdvice() {
        System.out.println("afterThrowingAdvice()");
    }
    public void afterReturningAdvice() {
        System.out.println("afterReturningAdvice()");
    }
}

 

 

 

applicationCTX.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">



    <aop:config>
        <aop:aspect id="Logger" ref="logAop">
            <aop:pointcut id="publicM" expression="within(com.Ex4.*)" />
            <aop:around pointcut-ref="publicM" method="LoggerAop" />
        </aop:aspect>
    </aop:config>

    <aop:config>
        <aop:aspect id="Logger" ref="logAop">
            <aop:pointcut id="publicM" expression="within(com.Ex4.*)" />
            <aop:before pointcut-ref="publicM" method="beforeAdvice" />
        </aop:aspect>
    </aop:config>

    <aop:config>
        <aop:aspect id="Logger" ref="logAop">
            <aop:pointcut id="publicM" expression="within(com.Ex4.*)" />
            <aop:after-returning pointcut-ref="publicM" method="afterReturningAdvice" />
        </aop:aspect>
    </aop:config>

    <aop:config>
        <aop:aspect id="Logger" ref="logAop">
            <aop:pointcut id="publicM" expression="within(com.Ex4.*)" />
            <aop:after-throwing pointcut-ref="publicM" method="afterThrowingAdvice" />
        </aop:aspect>
    </aop:config>

    <aop:config>
        <aop:aspect id="Logger" ref="logAop">
            <aop:pointcut id="publicM" expression="within(com.Ex4.*)" />
            <aop:after pointcut-ref="publicM" method="afterAdvice" />
        </aop:aspect>
    </aop:config>


</beans>

 

 

 

반응형

'네트워크' 카테고리의 다른 글

AOP 사용법  (0) 2022.10.27
AOP  (0) 2022.10.27
MVC 디자인 패턴  (0) 2022.10.13