- 이 문서는 구루비에서 작성하였습니다.
- 이 문서를 다른 블로그나 홈페이지에 게재하실 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^
- 출처 : http://wiki.gurubee.net/pages/viewpage.action?pageId=26740965&
- 구루비 지식창고의 모든 문서는 크리에이티브 커먼즈의 저작자표시-비영리-동일조건변경허락(BY-NC-SA) 라이선스에 따라 자유롭게 사용할 수 있습니다.
XML 스키마를 이용한 AOP 실습
- 실습 환경은 [STUDY:실습환경 Dynamic Web Project 설정] 을 참고한다.
- 예제 소스 SVN 주소 : https://dev.naver.com/svn/oracleclub/branches/dev-1.0.2-spring-ioc/
1. AOP 설정 XML 스키마
- 아래는 XML 스키마로 AOP를 설정한 예이다.
src/main/resources/spring/applicationContext-aop.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd" default-autowire="byName"> <!-- 비지니스 로직 BO 객체 --> <bean id="messageTarget" class="com.oracleclub.study.aop.target.MessageTarget"></bean> <!-- POJO로 구현된 Advice 빈 설정 --> <bean id="pojoAdvice" class="com.oracleclub.study.aop.advice.PojoAdvice"></bean> <!-- AOP 설정 --> <aop:config> <aop:aspect ref="pojoAdvice"> <aop:before method="commonMessage" pointcut="execution(* com.oracleclub.study.aop.target.MessageTarget.*())"/> <aop:after method="commonMessage" pointcut="execution(* com.oracleclub.study.aop.target.MessageTarget.mail*())"/> </aop:aspect> </aop:config> </beans>
- AOP 설정 XML 스키마에 대해서 알아보도록 하자
- <aop:config> : AOP 설정 정보임을 나타낸다.
- <aop:aspect> : Aspect(Advice+Pointcut)을 설정한다.
- <aop:pointcut> : Pointcut을 설정한다.
- <aop:around> : MethodInterceptor와 마찬가지로 메서드 호출 전/후 , 예외발생 등 모든 시점에 적용 가능한 Advice를 정의한다.
- <aop:before> : 메서드 실행 전에 적용되는 Before Advice를 설정한다.
- <aop:after-returning> : 메서드가 정상적으로 실행된 후에 적용되는 Advice를 정의한다.
- <aop:after-throwing> : 메서드가 예외를 발생시킬 때 적용되는 Advice를 정의한다. try-catch 블록에서 catch 블록과 비슷하다.
- <aop:after> : 메서드가 정상적으로 실행되는지 또는 예외를 발생시키는지 여부에 상관없이 적용되는 Advice를 정의한다. try-catch-finally 에서 finally 블록과 비슷하다.
Execution 명시자
- execution(public void set*(..)) : 리턴 타입이 void이고 메서드 이름이 set으로 시작하고, 파라미터가 0개 이상인 메서드 호출
- execution(* com.oracleclub.core..()) : com.oracleclub.core 패키지의 파라미터가 없는 모든 메서드 호출
- execution(* com.oracleclub.core...(..)) : com.oracleclub.core 패키지 및 하위 패키지에 있는 파라미터가 0개 이상인 메서드 호출
- execution(Integer com.oracleclub.core.AtricleBO.write(..)) : 리턴 타입이 Integer인 AtricleBO 인터페이스의 write() 메서드 호출
- execution(* get*( * )) : 이름이 get으로 시작하고 1개의 파라미터를 갖는 메서드 호출
- execution(* get*(.)) : get으로 시작하고 2개의 파라미터를 갖는 메서드 호출
목차 |
문서정보
- 이 문서는 구루비에서 작성하였습니다.
- 이 문서를 다른 블로그나 홈페이지에 게재하실 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^
- 출처 : http://wiki.gurubee.net/pages/viewpage.action?pageId=26740965&
- 구루비 지식창고의 모든 문서는 크리에이티브 커먼즈의 저작자표시-비영리-동일조건변경허락(BY-NC-SA) 라이선스에 따라 자유롭게 사용할 수 있습니다.