본문 바로가기

자바/SpringFrameWork

[스프링퀵스타트2-2] : 스프링 AOP (P. 164 ~ )

2.2 AOP 엘리먼트

 

" 앞 까지는 핵심관심, 횡단관심, 조인포인트, 포인트컷, 어드바이스(* 애스팩트), 위빙 등 AOP개념에 대해서 정리했다. 여기서는 AOP 의 스프링설정파일 엘리먼트 작성 방법에 대해서 배우겠다."  

"아래의 모든 엘리먼트들은 스프링 설정 파일내에서 사용되는 AOP관련 엘리먼트이다."

 

2.1.1 <aop:config>엘리먼트

: AOP 설정에서의 <aop:config>엘리먼트는 루트 엘리먼트이다. 

 

#첨부자료1. 스프링 설정파일 내에서 AOP 엘리멘트 설정 

<스프링설정파일 내의 AOP 설정>

2.2.2<aop:pointcut>엘리먼트

- 사용 위치 : <aop:config></aop:config>엘리먼트 내에서 사용 / <aop:aspect></aop:aspect>엘리먼트 내에서 사용

- 목적 : 포인트 컷을 지정하기 위함.

 

#첨부자료1. pointcut 설정 참고자료

 <bean id="log" class="com.springbook.biz.common.LogAdvice"/>
<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="log">
		<aop:before pointcut-ref="getPointcut" method="printLog"/>
	</aop:aspect>
</aop:config>  

1) "getPointcut"이라는 포인트 컷은 "com.springbook.biz" 패키지 중 Impl로 끝나는 클래스 내에서. get으로 시작하는 모든 메소드를 포인트 컷으로 지정하고 있다.

2) aspect 설정에서 <aop:before> 엘리먼트의 pointcut-ref 속성으로 포인트 컷을 참조 하고 있다.


2.2.3<aop:aspect>엘리먼트

- 사용 위치 : <aop:config></aop:config> 엘리먼트 내에서 사용

- 목적 : aspect(횡단메소드 지정 + 횡단메소드 어느시점에 작동되는지)

" 핵심관심(포인트컷 메소드)과 횡단 관심(어드바이스 메소드)을 결합하기 위해 사용한다. 가장 중요한 엘리먼트인 듯 .."

 

#첨부자료1. LogAdvice(횡단메소드)

package com.springbook.biz.common;

public class LogAdvice {
	public void printLog(){
		System.out.println("[공통 로그] 비즈니스 로직 수행 전 동작");
	}
}

#첨부자료2. 스프링설정파일

 <bean id="log" class="com.springbook.biz.common.LogAdvice"/>
<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="log">
		<aop:before pointcut-ref="getPointcut" method="printLog"/>
	</aop:aspect>
</aop:config>  

1) ref="getPointcut"으로 설정된 포인트컷 메소드가 실행 될 때, ref="log"라는 어드바이스 객체의 "printLog"가 실행 된다.

2) 실행 시점은 <aop:before> 이다. 

 

2.3 포인트컷 표현식

"조인포인트 중에 핵심관심으로 선택 된 것들이 포인트컷이다. 스프링설정파일 .xml 을 활용하여, 이 포인트컷을 정확하게 필터링 할 수 있다."

<포인트컷 표현식>

- 1. 리턴타입

- 2. 패키지

- 3. 클래스명

- 4. 메소드 및 매개변수