13. AOP(2)


AOP(2)


xmlを使用してaop設定を行うことができます.まずはSpringioのxmlを参照し、mavenに依存性を追加します.次に、まずbeanを作成し、aspectを作成し、作成したbeanにrefを追加します.最後に、切り込みポイントとadviceを定義します.
切り込み点の構文
実行→実行時実行
*->すべてのクラス、すべてのメソッド
(..) -> すべてのパラメータ


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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <aop:config>
        <aop:aspect id="myAspect" ref="aBean">
            <aop:pointcut id="businessService"
                          expression="execution(* kr.co.fast.cli.aop.Service.*(..))"/> <!-- point cut 정의 -->
            <aop:around pointcut-ref="businessService" method="aroundLog" />
            <!-- <aop:before pointcut-ref="businessService" method="beforeLog" /> beforeAdvice 정의
            <aop:after pointcut-ref="businessService" method="afterLog" /> afterAdvice 정의(무조건 로직 실행후 실행)
            <aop:after-returning pointcut-ref="businessService" method="afterReturningLog" /> afterReturningAdvice(로직이 잘 실행되면 그 후 실행) 정의
            <aop:after-throwing pointcut-ref="businessService" method="afterThrowingLog" /> afterThrowingAdvice(로직이 예외발생시 실행) 정의 -->
        </aop:aspect>
    </aop:config>

    <bean id="aBean" class="kr.co.fast.cli.aop.AopBean">
    </bean>

    <bean id="service" class="kr.co.fast.cli.aop.Service">
    </bean>

</beans>

Service

package kr.co.fast.cli.aop;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Service {
        public void log() {
            log.error(">>>> aop log");
        }
}

AopBean

package kr.co.fast.cli.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;

@Slf4j
public class AopBean {

    public void beforeLog() {
        System.out.println(">>>> before AOP Log!");
    }

    public void afterLog() {
        System.out.println(">>>> after AOP Log!");
    }

    public void afterReturningLog() {
        System.out.println(">>>> afterReturning AOP  Log!");
    }

    public void afterThrowingLog() {
        System.out.println(">>>> afterThrowing AOP  Log!");
    }

    public void aroundLog(ProceedingJoinPoint pjp) { // around는 ProceedingJoinPoint 필요! -> 이것 사용시 위에 것 모두 대체 가능!
        log.error(">>>> before AOP  Log!");
        try {
            Object proceed = pjp.proceed();
            log.error(">>>> returning AOP  Log!");
        } catch (Throwable throwable) {
            log.error(">>>> throwing AOP  Log!");

        }
        log.error(">>>> after AOP  Log!");
    }
}

Main

package kr.co.fast.cli.aop;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        Service service = context.getBean(Service.class);
        service.log();
        context.close();
    }
}