λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

Spring/Spring Quick Start

Advice λ™μž‘ μ‹œμ 

 

μŠ€ν”„λ§μ—μ„œλŠ” λ‹€μ„― κ°€μ§€μ˜ λ™μž‘ μ‹œμ μ„ μ œκ³΅ν•œλ‹€.

μ–΄λ“œλ°”μ΄μŠ€ λ©”μ†Œλ“œμ˜ λ™μž‘ μ‹œμ μ€ <aop:aspect> μ—˜λ¦¬λ¨ΌνŠΈ ν•˜μœ„μ— 각각 <aop:before>, <aop:after-returning>, <aop:after-throwing>, <aop:after>, <aop:around> μ—˜λ¦¬λ¨ΌνŠΈλ₯Ό μ΄μš©ν•˜μ—¬ μ§€μ •ν•œλ‹€.


1. <aop:before>

포인트컷으둜 μ§€μ •λœ λ©”μ†Œλ“œ 호좜 μ‹œ λ©”μ†Œλ“œκ°€ μ‹€ν–‰λ˜κΈ° 전에 μ²˜λ¦¬ν•  λ‚΄μš©λ“€μ„ κΈ°μˆ ν•˜κΈ° μœ„ν•΄ μ‚¬μš©λœλ‹€.

package com.springbook.biz.common;

public class BeforeAdvice {
	public void beforeLog(){
		System.out.println("[사전 처리] λΉ„μ¦ˆλ‹ˆμŠ€ 둜직 μˆ˜ν–‰ μ „ λ™μž‘");
	}
}
	<bean id="before" class="com.springbook.biz.common.BeforeAdvice"/>	

	<aop:config>
		<aop:pointcut id="allPointcut"
			expression="execution(* com.springbook.biz..*Impl.*(..))" />
		
		<aop:aspect ref="before">
			<aop:around pointcut-ref="allPointcut" method="beforeLog" />
		</aop:aspect>
		
	</aop:config>

2. <aop:after-returning>

포인트컷으둜 μ§€μ •λœ λ©”μ†Œλ“œκ°€ μ •μƒμ μœΌλ‘œ μ‹€ν–‰λ˜κ³  λ‚˜μ„œ κ²°κ³Ό 데이터λ₯Ό μ΄μš©ν•˜μ—¬ 사후 처리 λ‘œμ§μ„ μΆ”κ°€ν•  λ•Œ μ‚¬μš©ν•  수 μžˆλ‹€.

package com.springbook.biz.common;

public class AfterReturningAdvice {
	public void afterLog() {
		System.out.println("[사후 처리] λΉ„μ¦ˆλ‹ˆμŠ€ 둜직 μˆ˜ν–‰ ν›„ λ™μž‘");
	}
}
	<bean id="afterReturning" class="com.springbook.biz.common.AfterReturningAdvice"/>	

	<aop:config>
		<aop:pointcut id="getPointcut"
			expression="execution(* com.springbook.biz..*Impl.get*(..))" />
		
		<aop:aspect ref="afterReturning">
			<aop:after-returning pointcut-ref="getPointcut" method="afterLog" />
		</aop:aspect>
		
	</aop:config>

3. <aop:after-throwing>

포인트컷으둜 μ§€μ •λœ λ©”μ†Œλ“œκ°€ μ‹€ν–‰λ˜λ‹€κ°€ μ˜ˆμ™Έκ°€ λ°œμƒν•˜λŠ” μ‹œμ μ— λ™μž‘ν•œλ‹€.

package com.springbook.biz.common;

public class AfterThrowingAdvice {
	public void exceptionLog() {
		System.out.println("[μ˜ˆμ™Έ 처리] λΉ„μ¦ˆλ‹ˆμŠ€ 둜직 μˆ˜ν–‰ 쀑 μ˜ˆμ™Έ λ°œμƒ");
	}
}
	<bean id="afterThrowing" class="com.springbook.biz.common.AfterThrowingAdvice"/>
    
	<aop:config>
		<aop:pointcut id="allPointcut"
			expression="execution(* com.springbook.biz..*Impl.*(..))" />
		
		<aop:aspect ref=afterThrowing">
			<aop:after-throwing pointcut-ref="allPointcut" method="exceptionLog" />
		</aop:aspect>
		
	</aop:config>

4. <aop:after>

μ˜ˆμ™Έ λ°œμƒ 여뢀와 상관없이 무쑰건 μˆ˜ν–‰λ˜λŠ” μ–΄λ“œλ°”μ΄μŠ€λ₯Ό 등둝할 λ•Œ, After μ–΄λ“œλ°”μ΄μŠ€λ₯Ό μ‚¬μš©ν•œλ‹€.

package com.springbook.biz.common;

public class AfterAdvice {
	public void finallyLog() {
		System.out.println("[사후 처리] λΉ„μ¦ˆλ‹ˆμŠ€ 둜직 μˆ˜ν–‰ ν›„ 무쑰건 λ™μž‘");
	}
}
	<bean id="afterThrowing" class="com.springbook.biz.common.AfterThrowingAdvice"/>
	<bean id="after" class="com.springbook.biz.common.AfterAdvice"/>
    
	<aop:config>
		<aop:pointcut id="allPointcut"
			expression="execution(* com.springbook.biz..*Impl.*(..))" />
            
		<aop:aspect ref=afterThrowing">
			<aop:after-throwing pointcut-ref="allPointcut" method="exceptionLog" />
		</aop:aspect>
		<aop:aspect ref="after">
			<aop:after pointcut-ref="allPointcut" method="finallyLog" />
		</aop:aspect>
	</aop:config>

5. <aop:around>

λΉ„μ¦ˆλ‹ˆμŠ€ λ©”μ†Œλ“œ μ‹€ν–‰ μ „κ³Ό 후에 λͺ¨λ‘ λ™μž‘ν•˜λŠ” λ‘œμ§μ„ μ²˜λ¦¬ν•  λ•Œ μ‚¬μš©ν•œλ‹€.

pip.proceed() λ©”μ†Œλ“œ 호좜 μ•žμ— μž‘μ„±λœ μ½”λ“œλŠ” Before μ–΄λ“œλ°”μ΄μŠ€μ™€ λ™μΌν•˜κ²Œ λ™μž‘ν•˜κ³ , pip.proceed() λ©”μ†Œλ“œ 호좜 뒀에 μž‘μ„±λœ μ½”λ“œλŠ” After μ–΄λ“œλ°”μ΄μŠ€μ™€ λ™μΌν•˜κ²Œ λ™μž‘ν•œλ‹€.

package com.springbook.biz.common;

import org.aspectj.lang.ProceedingJoinPoint;

public class AroundAdvice {
	public Object aroundLog(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("[BEFORE]: λΉ„μ¦ˆλ‹ˆμŠ€ λ©”μ†Œλ“œ μˆ˜ν–‰ 전에 μ²˜λ¦¬ν•  λ‚΄μš©...");
		Object returnObj = pjp.proceed();
		System.out.println("[AFTER]: λΉ„μ¦ˆλ‹ˆμŠ€ λ©”μ†Œλ“œ μˆ˜ν–‰ 후에 μ²˜λ¦¬ν•  λ‚΄μš©...");
		return returnObj;
	}
}
	<bean id="around" class="com.springbook.biz.common.AroundAdvice"/>
    
	<aop:config>
		<aop:pointcut id="allPointcut"
			expression="execution(* com.springbook.biz..*Impl.*(..))" />
		
		<aop:aspect ref="around">
			<aop:around pointcut-ref="allPointcut" method="aroundLog" />
		</aop:aspect>
		
	</aop:config>

'Spring > Spring Quick Start' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

ν”„λ‘œνΌν‹° νŒŒμΌμ„ μ΄μš©ν•œ Spring JDBC  (0) 2021.01.07
Annotation 기반 AOP 적용  (0) 2021.01.07
JointPoint λ©”μ†Œλ“œ  (0) 2021.01.07
포인트컷 ν‘œν˜„μ‹  (0) 2021.01.07
AOP μ—˜λ¦¬λ¨ΌνŠΈ  (0) 2021.01.07
AOP μš©μ–΄  (0) 2021.01.07