Spring 에는 개발자는 핵심 로직과 그 외의 부분을 분리하여 개발 할 수 있도록 '관심사'에 대한 분리를 지원하며, 이를 '관점 지향 프로그래밍(AOP)'라고 한다.
관심사는 개발자가 염두에 두어야 하는 것을 의미하며,
특정 로직에 대한 예외에 대한 검사, 값에 대한 추적, 로직의 사전, 사후 처리 등이 있다.
- Proxy : Target의 호출, 필요에 따라 다른 Target을 호출하도록 수동 혹은 자동으로 작성됨
- Advice : 관심사에 해당하는 메소드 등
- JoinPoint : Target이 가진 메소드의 종류 중 하나, 관심사가 호출 될 수 있도록 동작함
단순히 보자면, 어느 웹 페이지의 기능을 구성하는 순수한 로직(Target)에 관심사에 해당하는 특수한 메소드(Advice)가 Proxy에 의해 호출(JoinPoint를 이용)되어 사용된다
-Pointcut : 관심사와 비즈니스 로직의 결합 지점
관심사에 대한 Advice는 동작 위치 및 시간에 따라 다음과 같이 구분된다.
- Before Advice : Target 의 JoinPoint 호출 전 실행 되는 코드
- After Returning Advice : 모든 실행 정상 종료 후 실행되는 코드
- After Throwing Advice : 예외 발생 후 실행되는 코드
- After Advice : 예외 상관 없이 실행 종료 후 실행되는 코드
- Around Advice : 메서드의 실행 제어가 가능함, 직접 대상의 메서드를 호출하고 결과나 예외 처리 가능
Spring(ver.3 이상) 에서 Advice는 어노테이션 만으로로 어떤 Target에 어떤 advice를 적용 할지 설정(JoinPoint의 설정)이 가능하다.
execution(@execution) : 메서드를 기준으로 Pointcut 설정
whitin(@whitin) : 특정한 타입(클래스)을 기준으로 Pointcut을 설정
this : 주어진 인터페이스를 구현한 객체를 대상으로 Pointcut을 설정
args(@args) : 특정한 파라미터를 가지는 대상들만을 Pointcut으로 설정
@annotation : 특정 어노테이션이 적용된 대상들만을 Pointcut으로 설정
※사전 준비
root-context.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- Root Context: defines shared resources visible to all other web components --> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.test.service"></context:component-scan> <context:component-scan base-package="com.test.aop"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans> | cs |
pom.xml
-스프링 및 Java 설정
| - pom.xml 상단에 위치 |
- Test 및 AOP Maven
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <!-- Test --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.7.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.0</version> <scope>provided</scope> </dependency> <!-- AOP --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.0</version> </dependency> | cs |
AOP 파일에 대해 명시를 위해서는 @Aspect 의 기입이 필요하다
① Advice 가 동작하는 시점 : before - target 실행 전
② 메서드를 기준으로 Pointcut 설정
③ 대상 메소드 명 기입
@before
- target 수행 전 사전에 먼저 수행됨을 명시
- execution() 내부에 대상이 되는 패키지를 기입(띄어쓰기 주의)
execution(* com.test.service.SampleService*.doAdd(String, String))
@args
- 특정 메소드에 대해 특정 매개변수의 추적을 수행
- 주로 execution 과 사용되며 "&&args(매개변수1, 매개변수2, .... )" 형식으로 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | @Around("execution(* com.test.service.SampleService*.*(..))") public Object logTime(ProceedingJoinPoint pjp) { //수행 시작 시간 long start = System.currentTimeMillis(); //수행 메소드의 객체명 가져옴 log.info("Target : "+pjp.getTarget()); //수행 메소드의 매개변수 log.info("param : "+Arrays.toString(pjp.getArgs())); Object result = null; try { result = pjp.proceed();//수행해야할 Advice 수행 } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } long end = System.currentTimeMillis();//종료 시점 시간 log.info("time : "+(end-start)); return result; } | cs |


댓글
댓글 쓰기