네트워크

AOP

GaeGim 2022. 10. 27. 19:14
반응형
  • 관점 지향 프로그램
  • OOP로 독립 분리하기 어려운 부가 기능을 모듈화하는 방식

 

 

- Aspect : 공통 기능

- Advice : Aspect의 기능 자체

- Joinpoint : Advice를 적용해야 하는 부분 (※ 스프링에서는 메서드만 해당)

- Pointcut : Joinpoint의 부분으로, 실제 Advice가 적용된 부분

- Weaving : Advice를 핵심 기능에 적용하는 행위

 

 

 

 

  • 스프링에서 AOP 구현 방법

- XML 스키마 기반 구현

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));
        }
    }
}

 

 

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>
    
    
    </beans>

 

 

 

 

 

- @Aspect 어노테이션 기반 구현

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));
        }
    }
}

 

 

applicationCTX.xml
 <aop:aspectj-autoproxy />
    <bean id="logAop" class="com.Ex4.logAop"/>
반응형

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

AOP 사용법  (0) 2022.10.27
AOP - Advice 종류  (0) 2022.10.27
MVC 디자인 패턴  (0) 2022.10.13