AOP 용어 | 설명 |
조인포인트(Joinpoint) | 조인포인트는 클라이언트가 호출하는 모든 비즈니스 메소드를 의미하며, 보통 Impl 클래스의 모든 메서드를 조인포인트라고 한다. 또한 조인포인트는 포인트컷 대상 또는 포인트컷 후보로도 불리며, 설정된 expression에서 지정한 클래스의 모든 메서드들이 조인포인트로 간주된다. |
포인트컷(Point Cut) | 포인트컷은 실제 aop가 적용될 클래스 메소드들이다. 조인포인트에서 필터링해서 원하는 메소드에만 횡단 공통기능을 수행시키기 위해서 사용한다. |
어드바이스(Advice) | 횡단 관심에 해당하는 공통 기능의 코드이다. 독립된 클래스의 메소드로 작성한다. |
위빙(Weaving) | 포인트컷으로 지정한 핵심 관심 메소드가 호출될 때, 어드바이스에 해당하는 횡단 관심 메소드가 삽입되는 과정을 의미한다. 이를 통해 비즈니스 메소드를 수정하지 않고도 횡단 관심에 해당하는 기능을 추가하거나 변경이 가능해진다. |
애스팩트(Aspect) | 포인트컷과 어드바이스의 결합이다. 어떤 포인트컷 메소드에 대해 어떤 어드바이스 메소드를 실행할지 결정한다. |
Point Cut은 특정 메소드에서만 횡단 관심에 해당하는 공통 기능을 수행시키기 위해서 사용한다. 예를 들어 transaction을 처리하는 공통 기능을 만들었을 때 등록, 수정, 삭제 기능에는 트랜잭션이 동작해야하지만 조회 기능에는 트랜잭션이 동작(commit, rollback)할 필요없다. Point Cut을 이용하면 메소드가 포함된 클래스와 패키지는 물론, 메소드 시그니처까지 정확하게 지정 가능하다.
<?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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="target" class="spring.aop.entity.NewlecExam" p:kor="1" p:eng="1" p:com="1" p:math="1"/>
<bean id="logAroundAdvice" class="spring.aop.advice.LogAroundAdvice"/>
<bean id="logBeforeAdvice" class="spring.aop.advice.LogBeforeAdvice"/>
<bean id="logAfterReturningAdvice" class="spring.aop.advice.LogAfterReturningAdvice"/>
<bean id="logAfterThrowingAdvice" class="spring.aop.advice.LogAfterThrowingAdvice"/>
// 추가
<bean id="classicPointCut" class="org.springframework.aop.support.NameMatchMethodPointcut">
<property name="mappedName" value="total"/> // total 메서드만 사용
</bean>
// 추가, 위의 classicPointCut과 아래의 포인트컷(logBeforeAdvice)를 연결해주는 역할
<bean id="classicBeforeAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="logBeforeAdvice"/>
<property name="pointcut" ref="classicPointCut"/>
</bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="target" />
<property name="interceptorNames">
<list>
<value>logAroundAdvice</value>
<value>classicBeforeAdvisor</value> // 변경, <value>logBeforeAdvice</value>
<value>logAfterReturningAdvice</value>
<value>logAfterThrowingAdvice</value>
</list>
</property>
</bean>
</beans>
package spring.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.aop.entity.Exam;
import spring.aop.entity.NewlecExam;
public class Program {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring/aop/setting.xml"); //추가
Exam proxy = (Exam) context.getBean("proxy");
System.out.printf("total is %d\n", proxy.total());
System.out.printf("avg is %f\n", proxy.avg());
}
}
// <출력> (total만 "앞에서 실행될 로직"이 실행됌 avg는 x)
//앞에서 실행될 로직
//returnValue:4, method:total
//214ms 시간이 걸렸습니다.
//total is 4
//returnValue:1.0, method:avg
//202ms 시간이 걸렸습니다.
//avg is 1.000000
참고자료
'🖥️ Backend > Spring' 카테고리의 다른 글
[Spring] 21.Spring AOP(XML, Point Cut②) (0) | 2025.01.02 |
---|---|
[Spring] 19.Spring AOP(XML, AfterThrowingAdvice) (0) | 2024.12.31 |
[Spring] 18.Spring AOP(XML, AfterReturningAdvice) (0) | 2024.12.31 |
[Spring] 17.Spring AOP(XML, BeforeAdvice) (0) | 2024.12.31 |
[Spring] 16.Spring AOP(XML, AroundAdvice) (0) | 2024.12.31 |