AOP활용 (xml 기반)

이은영·2022년 6월 15일
0

웹프레임워크

목록 보기
3/4

1. 어드바이스(Advice)

어드바이스는 각 포인트컷에 삽입되어 동작할 횡단관심에 해당되는 공통기능

동작시점

<aop:before> : 비즈니스 메소드 실행 전 무조건 실행
<aop:after> : 비즈니스 메소드 실행 후 무조건 실행 ( try-catch-finally )
<aop:around> : 비즈니스 메소드 실행 전후에 실행
<aop:after-returning> : 비즈니스 메소드 성공적인 실행(예외없이) 후에 동작
<aop:after-throwing> : 비즈니스 메소드 실행 중에 예외 발생시에 동작(try-catch에서 catch에서 발생)

before

<context:component-scan base-package="com.spring.aopex2"/>
<bean id = "before" class = "com.spring.aopex2.BeforeAdvice" /> <!--  before 객체가 만들어짐  -->
<aop:config>
	<aop:pointcut expression="execution(* com.spring.aopex2..*Impl.insertBoardDo(..))" id="insertDoPointcut"/>
	<aop:aspect ref = "before">
		<aop:before method="beforeLogDo" pointcut-ref="insertDoPointcut"/>
	</aop:aspect>  
</aop:config>

after

	<bean id = "after" class = "com.spring.aopex2.AfterAdvice" />
	
	<aop:config>
		<aop:pointcut expression = "execution(* com.spring.aopex2..*Impl.insert*(..))" id = "insertPointcut"/>
		<aop:aspect ref = "after">
			<aop:after method = "afterAdvice" pointcut-ref = "insertPointcut"/>
		</aop:aspect>
	</aop:config>

around

	<bean id = "around" class = "com.spring.aopex2.AroundAdvice" />
	
	<aop:config>
		<aop:pointcut expression = "execution(* com.spring.aopex2..*Impl.insert*(..))" id = "insertPointcut"/>
		<aop:pointcut expression="execution(* com.spring.aopex2..*Impl.insertBoardReturn(..))" id="insertReturnPointcut"/>

		<aop:aspect ref = "around">
			<aop:around method = "aroundAdvice" pointcut-ref="insertPointcut" />
		</aop:aspect>
		
		<aop:aspect ref = "around">
			<aop:around method = "aroundAdviceDo" pointcut-ref="insertReturnPointcut" />
		</aop:aspect>
	</aop:config>

2. 조인포인트

횡단관심에 해당하는 어드바이스 메소드 구현시에,
포인트컷에 해당되는 메소드의 정보(메소드 이름, 매개변수의 값등)가 필요할 수 있음
디버깅, 로그, 예외처리 등의 포인트 컷 메소드의 정보가 필요함

❗️ Main로직의 인자들이 잘 들어오고 싶은지 확인, 값이 잘 들어오는지 확인하고 싶어서 사용

조인포인트 메서드

after-returning

<bean id = "afterReturning" class = "com.spring.aopex2.AfterReturningAdvice" />
	<aop:config>
		<aop:pointcut expression = "execution(* com.spring.aopex2..*Impl.insert*(..))" id = "insertPointcut"/>
		<aop:pointcut expression="execution(* com.spring.aopex2..*Impl.update*(..))" id="updatePointcut"/>
		<aop:pointcut expression="execution(* com.spring.aopex2..*Impl.insertBoardReturn(..))" id="insertReturnPointcut"/>
		<!-- <aop:aspect ref = "afterReturning">
			<aop:before method="afterReturningAdviceLog" pointcut-ref = "insertPointcut" />
			<aop:after-returning method="afterReturningAdviceLog" pointcut-ref="updatePointcut"/> 
		</aop:aspect> -->
		 <aop:aspect ref = "afterReturning">
			<aop:after-returning method = "afterReturningAdviceLogDo" 
			pointcut-ref="insertReturnPointcut" 
			returning="returnObj" />
		</aop:aspect> 
	</aop:config>

after-throwing

	<bean id = "afterThrowing" class = "com.spring.aopex2.AfterThrowingAdvice" />
	<aop:config>
		<aop:pointcut expression = "execution(* com.spring.aopex2..*Impl.insert*(..))" id = "insertPointcut"/>
		<aop:pointcut expression="execution(* com.spring.aopex2..*Impl.insertBoardExcept(..))" id="insertExceptPointcut"/>
		<!-- <aop:aspect ref="afterThrowing"> 
			<aop:after-throwing method="afterThrowingAdviceLog" pointcut-ref="insertPointcut"/>
		</aop:aspect> -->
		<aop:aspect ref = "afterThrowing">
			<aop:after-throwing method = "afterThrowingLogExcept" 
								pointcut-ref="insertExceptPointcut" 
								throwing="exceptObj" />
		</aop:aspect>  
	</aop:config>
profile
성장중인 개발자 (머리속의 생각을 글로 옮기는 연습을 하고 있습니다.)

0개의 댓글