μ€νλ§μμλ λ€μ― κ°μ§μ λμ μμ μ μ 곡νλ€.
μ΄λλ°μ΄μ€ λ©μλμ λμ μμ μ <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 |