framework/spring AOP(κ΄μ μ§ν₯)
[springAOP] 8. μ΄λ Έν μ΄μ κΈ°λ°μ AOP λ§λ€μ΄ μ¬μ©νκΈ° | Advisor ν΄λμ€
jeri
2024. 7. 31. 16:18
λ°μν
01. πSpring Bean Configuration File
[07-5_aopAnnotation.xml]
- bean , context , aop κΈ°λ₯κΉμ§ λͺ¨λ μ΄μ©νλ―λ‘ 3κ° λ€ μ€μ ν΄μ μμ±ν΄μΌν¨!!
<?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>"
xmlns:context="<http://www.springframework.org/schema/context>"
xsi:schemaLocation="<http://www.springframework.org/schema/beans> <http://www.springframework.org/schema/beans/spring-beans.xsd>
<http://www.springframework.org/schema/context> <http://www.springframework.org/schema/context/spring-context.xsd>
<http://www.springframework.org/schema/aop> <http://www.springframework.org/schema/aop/spring-aop.xsd>">
<!-- μ΄μ ν΄λμ€μ @ μ΄λ
Έν
μ΄μ
μ μ¬μ©ν΄μ μ€νλ§ λΉμΌλ‘ λ±λ‘ κ°λ₯ν΄μ§! -->
<context:component-scan base-package="xyz.itwill07.aop"/>
<!-- aspectj-autoproxy : Spring Annotationλ₯Ό μ΄μ©νμ¬ AOP κΈ°λ₯μ μ 곡νκΈ° μν μλ¦¬λ¨ΌνΈ -->
<!-- => AOP κ΄λ ¨ Annotationμ μ¬μ©νμ¬ ν΅μ¬κ΄μ¬μ½λμ ν‘λ¨κ΄μ¬μ½λλ₯Ό μ½μ
νμ¬ μ€νλλλ‘ μ€μ -->
<aop:aspectj-autoproxy/>
</beans>
02. [ν΅μ¬κ΄μ¬λͺ¨λ]
AopAnnotationBean.java
package xyz.itwill07.aop;
import org.springframework.stereotype.Component;
//Component μ΄λ
Έν
μ΄μ
μ μν΄ Spring BeanμΌλ‘ λ±λ‘λ¨
@Component
public class AopAnnotationBean {
public void display1() {
System.out.println("### AopAnnotationBean ν΄λμ€μ display1() λ©μλ νΈμΆ ###");
}
public void display2() {
System.out.println("### AopAnnotationBean ν΄λμ€μ display2() λ©μλ νΈμΆ ###");
}
public void display3() {
System.out.println("### AopAnnotationBean ν΄λμ€μ display3() λ©μλ νΈμΆ ###");
throw new RuntimeException(); //after-throwing μ΄λ
Έν
μ΄μ
μ μ΄μ©νκΈ° μν΄ μΈμμ μμΈλ°μ
}
}
03. [ν‘λ¨κ΄μ¬λͺ¨λ] : Advisorν΄λμ€
AopAnnotationAdvice.java
package xyz.itwill07.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
//Advisor ν΄λμ€ : JoinPoint κΉμ§ λͺ¨λ μ§μ λ ν΄λμ€
@Component
//@Aspect : ν΅μ¬κ΄μ¬μ½λμ ν‘λ¨κ΄μ¬μ½λλ₯Ό μ½μ
νμ¬ μ€ννκΈ° μν κΈ°λ₯μ μ 곡νλ μ΄λ
Έν
μ΄μ
//=> Spring Bean Configuration Fileμ aspect μ리먼νΈμ μ μ¬ν κΈ°λ₯ μ 곡
//=> Spring Bean Configuration Fileμ aspectj-autoproxy μλ¦¬λ¨ΌνΈ μΆκ°ν΄μΌ μ¬μ© κ°λ₯
@Aspect
public class AopAnnotationAdvice {
public static final Logger logger=LoggerFactory.getLogger(AopAnnotationBean.class);
//1.
//@Pointcut : νκ²λ©μλλ₯Ό μ§μ νκΈ° μν μ΄λ
Έν
μ΄μ
//=> λ©μλ νΈμΆμ μ΄μ©νμ¬ νκ²λ©μλλ₯Ό μ§μ ν PointCut ννμμ μ 곡νκΈ° μν΄ μ¬μ©
// => Spring Bean Configuration Fileμ pointcut μ리먼νΈμ μ μ¬ν κΈ°λ₯ μ 곡
//value μμ± : νκ²λ©μλλ₯Ό μ§μ νκΈ° μν PointCut ννμμ μμ±κ°μΌλ‘ μ€μ
// => λ€λ₯Έ μμ±μ΄ μλ κ²½μ° μμ±κ°λ§ μ€μ κ°λ₯
@Pointcut("within(xyz.itwill07.aop.AopAnnotationBean)")
public void aopPointCut() {}
//2.
//Before Advice λ©μλ
//@Before : ν΅μ¬κ΄μ¬μ½λ μ€ν μ μ ν‘λ¨κ΄μ¬μ½λλ₯Ό μ€ννλ κΈ°λ₯μ μ 곡νλ μ΄λ
Έν
μ΄μ
// => Spring Bean Configuration Fileμ before μ리먼νΈμ μ μ¬ν κΈ°λ₯ μ 곡
//value μμ± : νκ²λ©μλλ₯Ό μ§μ νκΈ° μν PointCut ννμμ μμ±κ°μΌλ‘ μ€μ
// => λ€λ₯Έ μμ±μ΄ μλ κ²½μ° μμ±κ°λ§ μ€μ κ°λ₯
//@Before("within(xyz.itwill07.aop.AopAnnotationBean)")
//value μμ±κ°μΌλ‘ @Pointcut μ΄λ
Έν
μ΄μ
μ μ¬μ©ν λ©μλλ₯Ό νΈμΆνλ©΄ λ±λ‘λ PointCut ννμμ μ 곡λ°μ μ¬μ© κ°λ₯
@Before("aopPointCut()")
public void beforeLog() {
logger.info("[before]ν΅μ¬κ΄μ¬μ½λ μ€ν μ μ½μ
λμ΄ μ€νλ ν‘λ¨κ΄μ¬μ½λ");
}
//3.
//After Advice λ©μλ
//@After : ν΅μ¬κ΄μ¬μ½λ μ€ν νμ 무쑰건 ν‘λ¨κ΄μ¬μ½λλ₯Ό μ€ννλ κΈ°λ₯μ μ 곡νλ μ΄λ
Έν
μ΄μ
// => Spring Bean Configuration Fileμ after μ리먼νΈμ μ μ¬ν κΈ°λ₯ μ 곡
//@After(value = "within(xyz.itwill07.aop.AopAnnotationBean)")
@After("aopPointCut()")
public void afterLog() {
logger.info("[after]ν΅μ¬κ΄μ¬μ½λ μ€ν ν 무쑰건 μ½μ
λμ΄ μ€νλ ν‘λ¨κ΄μ¬μ½λ");
}
//4.
//After Returning Advice λ©μλ
//@AfterReturning : ν΅μ¬κ΄μ¬μ½λκ° μ μμ μΌλ‘ μ€νλ ν ν‘λ¨κ΄μ¬μ½λλ₯Ό μ€ννλ κΈ°λ₯μ μ 곡νλ μ΄λ
Έν
μ΄μ
// => Spring Bean Configuration Fileμ after-returning μ리먼νΈμ μ μ¬ν κΈ°λ₯ μ 곡
//@AfterReturning("aopPointCut()")
//returning μμ± : νκ²λ©μλμ λ°νκ°μ μ μ₯νκΈ° μν λ§€κ°λ³μμ μ΄λ¦μ μμ±κ°μΌλ‘ μ€μ
@AfterReturning(value="aopPointCut()", returning="object")
public void afterReturningLog(Object object) {
logger.info("[after-returning]ν΅μ¬κ΄μ¬μ½λκ° μ μμ μΌλ‘ μ€νλ ν μ½μ
λμ΄ μ€νλ ν‘λ¨κ΄μ¬μ½λ");
}
//5.
//After Throwing Advice λ©μλ
//@AfterThrowing : ν΅μ¬κ΄μ¬μ½λ μ€νμ μμΈκ° λ°μλ κ²½μ° ν‘λ¨κ΄μ¬μ½λλ₯Ό μ€ννλ κΈ°λ₯μ μ 곡νλ μ΄λ
Έν
μ΄μ
// => Spring Bean Configuration Fileμ after-throwing μ리먼νΈμ μ μ¬ν κΈ°λ₯ μ 곡
//throwing μμ± : νκ²λ©μλμμ λ°μλ μμΈλ₯Ό μ μ₯νκΈ° μν λ§€κ°λ³μμ μ΄λ¦μ μμ±κ°μΌλ‘ μ€μ
@AfterThrowing(value="aopPointCut()", throwing ="exception" )
public void afterThrowingLog(Exception exception) {
logger.info("[after-throwing]ν΅μ¬κ΄μ¬μ½λ μ€νμ μμΈκ° λ°μλλ©΄ μ½μ
λμ΄ μ€νλ ν‘λ¨κ΄μ¬μ½λ");
}
//6.
//Around Advice λ©μλ
//@Around : ν΅μ¬κ΄μ¬μ½λ μ€ν μ κ³Ό νμ ν‘λ¨κ΄μ¬μ½λλ₯Ό μ€ννλ κΈ°λ₯μ μ 곡νλ μ΄λ
Έν
μ΄μ
// => Spring Bean Configuration Fileμ around μ리먼νΈμ μ μ¬ν κΈ°λ₯ μ 곡
@Around("aopPointCut()")
public Object aroundLog(ProceedingJoinPoint joinPoint) throws Throwable {
logger.info("[around]ν΅μ¬κ΄μ¬μ½λ μ€ν μ μ μ½μ
λμ΄ μ€νλ ν‘λ¨κ΄μ¬μ½λ");
Object object=joinPoint.proceed();
logger.info("[around]ν΅μ¬κ΄μ¬μ½λ μ€ν νμ μ½μ
λμ΄ μ€νλ ν‘λ¨κ΄μ¬μ½λ");
return object;
}
}
04. μ€ννλ‘κ·Έλ¨
AopAnnotationApp.java
package xyz.itwill07.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopAnnotationApp {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("07-5_aopAnnotation.xml");
AopAnnotationBean bean = context.getBean("aopAnnotationBean",AopAnnotationBean.class);
System.out.println("======================================================");
bean.display1();
System.out.println("======================================================");
bean.display2();
System.out.println("======================================================");
bean.display3();
System.out.println("======================================================");
((ClassPathXmlApplicationContext)context).close();
}
}

λ°μν