[IT]/Spring

Spring.AOP.around

givemebro 2021. 5. 25. 11:20
반응형
package org.kosta.aop;

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * 횡단 관심사항을 정의한 클래스<br>
 * around advice를 테스트 한다<br>
 * around advice는 4가지 advice를 모두 적용할 수 있는 advice다.<br>
 * (before, after, after-returning, after-throwing)<br>
 * 
 * @author broth
 *
 */
public class AroundLoggingService {
	public Object logging(ProceedingJoinPoint point) throws Throwable {
		// <before>
		System.out.println("***AOP 적용 before advice***");
		// </before>

		// 실제 core 메서드가 전달받은 매개변수의 데이터를 확인해본다
		Object params[] = point.getArgs();
		for (int i = 0; i < params.length; i++) {
			System.out.println("AOP에서 확인한 매개변수정보:*:" + params[i]);

		}

		// <now>
		// proceed(): 실제 AOP 적용 대상 메서드를 실행
		Object retValue = point.proceed();
		// </now>

		// <after>
		System.out.println("***AOP 적용 after advice***");
		// </after>
		return retValue;
	}

}
<?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-4.3.xsd">

	<bean id="personService" class="org.kosta.model.PersonService"></bean>
	<bean id="productService" class="org.kosta.model.ProductService"></bean>
	<!-- 횡단 관심사를 정의한 클래스 -->
	<bean id="aroundLogging"
		class="org.kosta.aop.AroundLoggingService"></bean>
	<aop:config>
		<aop:aspect ref="aroundLogging">
			<aop:pointcut expression="within(org.kosta.model.*)"
				id="pt" />
			<aop:around method="logging" pointcut-ref="pt" />
		</aop:aspect>
	</aop:config>
</beans>
반응형