Target : 실제 로직을 수행할 대상
Advice : 추가 실행되어야 할 기능들
Proxy : target + advice
Weaving : 격자 형태로 짜다
target 을 통해 proxy를 만드는 자체를 의미
Join Point : Target 의 모든 메서드가 Join Point
Point Cut : Advice 를 적용할 타겟의 메서드를 선별하는 정규표현식
Aspect : Advice + Point Cut
싱글톤 형태의 객체로 존재
Advisor : aspect랑 동일
Spring AOP 에서만 사용하는 용어
Spring의 AOP
스프링의 advice는 자바로 작성
스프링의 aspect 실행시간에 만들어진다.
프록시는 메서드를 오버라이드를 해서 쓴다.
호출할때 훔쳐감!
그래서 final 적용시키면 훔쳐가지 못 해서 오류 뜸!
around advice
before advice : 원래 해야할 join 포인트 이전에 처리한다.
after returning advice
throws advice
after advice
Myadvice.java
package edu.biz.aop3;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class Myadvice implements MethodBeforeAdvice{
public void before(Method method, Object[] args, Object target) throws Throwable {
// TODO Auto-generated method stub
System.out.println("사전 어드바이스");
}
}
MyBean.java
package edu.biz.aop3;
import lombok.Setter;
public class MyBean {
@Setter
private MyDependency dependency;
public void Run(){
dependency.hello();
dependency.goodbye();
}
}
MyDependency.java
package edu.biz.aop3;
public class MyDependency {
public void hello(){
System.out.println("안녕");
}
public void goodbye(){
System.out.println("ㅂ2");
}
}
aop3.xml
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<bean class="edu.biz.aop3.Myadvice" id="myAdvice"></bean>
<bean class="edu.biz.aop3.MyDependency" id="myDependency"></bean>
<bean class="edu.biz.aop3.MyBean" p:dependency-ref="myDependency1" id="myBean1"></bean>
<bean class="edu.biz.aop3.MyBean" p:dependency-ref="myDependency2" id="myBean2"></bean>
<bean class="org.springframework.aop.support.DefaultPointcutAdvisor" id="advisor">
<property name="advice" ref="myAdvice"/>
<property name="pointcut">
<bean class="org.springframework.aop.aspectj.AspectJExpressionPointcut">
<property name="expression" value="execution(* hello(..))"/>
</bean>
</property>
</bean>
<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="myDependency1">
<property name="target" ref="myDependency"/>
<property name="interceptorNames">
<list><value>myAdvice</value></list>
</property>
</bean>
<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="myDependency2">
<property name="target" ref="myDependency"/>
<property name="interceptorNames">
<list><value>myAdvice</value></list>
</property>
</bean>
</beans>
'자바 풀스택 공부' 카테고리의 다른 글
Day 67 ~ 73. 서버 구현 과제 (0) | 2022.04.20 |
---|---|
Day 66. [Spring] ORM (Object Relational Mapping) (0) | 2022.04.08 |
Day 63. [Spring/Java] porm.xml 적용, File 입출력 (0) | 2022.04.05 |
Day 61. [Spring, PL/SQL] Spring, ROLLBACK, SAVEPOINT, 함수 선언 (0) | 2022.04.01 |
Day 60. [PL/SQL] CASE, WHILE, FOR, LOOP (0) | 2022.03.31 |
댓글