본문 바로가기
자바 풀스택 공부

Day 65. [Spring] AOP_관점 지향 프로그래밍

by seung_nari 2022. 4. 7.

출처 https://3months.tistory.com/74

 

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>

댓글