AOP(Aspected-Oriented Programming)
spring
07/08/2021
๐ฟ AOP(Aspected-Oriented Programming)
- ์คํ๋ง๊ณผ ๋ณ๊ฐ๋ก ๋ฐฉ๋ฒ๋ก ์
- OOP๋ ์ฌ์ฉ์ ๊ด์ ์์์ ์ฃผ ์ ๋ฌด ๋ก์ง์ ์์ฃผ๋ก ๊ตฌํํ๋ ๊ฒ. ๋ฐ๋ฉด AOP ๋ ์ฃผ ์ ๋ฌด ๋ก์ง์ ๊ตฌํํจ์ ์์ด์ ๊ฐ๋ฐ์๋ ์ด์์ ๋ฐ ๊ด๋ฆฌ์๊ฐ ํ์์ ๋ฐ๋ผ์ ์ฝ๋๋ฅผ ๋ผ์๋ฃ๊ฒ ๋๋ ๋ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ(ex. ๋ก๊ทธ์ฒ๋ฆฌ, ํธ๋ ์ ์ ๋ฑ...)์ ๋ชจ๋ํํ๋ ๊ฒ.
- ์ฆ, ์ฌ์ฉ์ ๊ด์ , ๊ฐ๋ฐ์ ๊ด์ , ์ด์์ ๊ด์ , ๋ฑ๋ฑ..์ฌ๋ฌ ๊ด์ ๋ค์ ๊ณ ๋ คํด์ ๋ก์ง์ ๋ถ๋ฆฌํ๊ณ ๋ชจ๋ํํ๋ ๋ฐฉ๋ฒ๋ก ์ด๊ธฐ ๋๋ฌธ์ Aspected ๊ด์ ์งํฅ์ ํ๋ก๊ทธ๋จ์ด์ด๋ผ๊ณ ํ๋ค.
Concern - Primary(Core) Concern & Cross-cutting concern
- primary(Core) Concern : ํต์ฌ ๋ก์ง
- Cross-cutting Concern : ํต์ฌ๋ก์ง์ ์ ์๋๋ก ๊ฐ์ธ๊ณ ์๋ ๋ถ๊ฐ์ ์ธ ๊ณตํต ๋ก์ง
AOP ๊ตฌํ ๋ฐฉ์
- ํ๋ก์๋ก ์์๊ณผ ๋ถ๊ฐ์์ ์ ํตํด AOP๋ฅผ ๊ตฌํํ๋ค.
Proxy
1) ํ๋ก์๋? ๊ธฐ์กด ์ฝ๋์ ์ํฅ์ ์ฃผ์ง ์๊ณ ํ๊น์ ๊ธฐ๋ฅ์ ํ์ฅํ๊ณ ์ ๊ทผ๋ฐฉ๋ฒ์ ์ ์ดํ๋ ๋ฐฉ๋ฒ
2) ํ๋ก์์ ์ญํ
ํ๊ฒ์ผ๋ก ์์ฒญ์ ์์
๋ถ๊ฐ๊ธฐ๋ฅ์ ์ํ
ํ๋ก์ํฉํ ๋ฆฌ์๊ฒ ์ธํฐํ์ด์ค ์ ๋ณด๋ฅผ ์ ๊ณตํด์ฃผ๋ฉด ํด๋น ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค์ ์ค๋ธ์ ํธ๋ฅผ ์๋์ผ๋ก ๋ง๋ค์ด์ค๋ค
์ธํฐํ์ด์ค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก Proxy๋ฅผ ์์ฑํด์ฃผ๋ ๋ฐฉ์
์์ ์๋ฐ๋ก proxy ๋ง๋ค๊ธฐ
JAVA// ํ๊นํด๋์ค์ ์ธํฐํ์ด์คpackage noums.aop.entity;public interface Exam {public int total();public float avg();}JAVA// ํ๊น ํด๋์คpackage noums.aop.entity;public class ExamImpl implements Exam {private int kor;private int eng;private int math;public ExamImpl() {// TODO Auto-generated constructor stub}public ExamImpl(int kor, int eng, int math) {this.kor = kor;this.eng = eng;this.math = math;}public int getKor() {return kor;}public void setKor(int kor) {this.kor = kor;}public int getEng() {return eng;}public void setEng(int eng) {this.eng = eng;}public int getMath() {return math;}public void setMath(int math) {this.math = math;}@Overridepublic int total() {// ๋ถ๊ฐ๋ก์ง// long start = System.currentTimeMillis();// ํต์ฌ๋ก์งint result = kor+eng+math;// try {// Thread.sleep(200);// } catch (InterruptedException e) {// // TODO Auto-generated catch block// e.printStackTrace();// }//// // ๋ถ๊ฐ๋ก์ง// long end = System.currentTimeMillis();//// String message = (end-start) + "ms ์๊ฐ ์์ ";// System.out.println(message);return result;}@Overridepublic float avg() {// ํต์ฌ์ ๋ฌดfloat result = total() / 3.0f;return result;}
}```
```javapackage noums.aop;
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;
import noums.aop.entity.Exam;import noums.aop.entity.ExamImpl;
public class Project {
public static void main(String[] args) { Exam exam = new ExamImpl(1,1,1); // ํ๊น์ ์ธํฐํ์ด์ค๋ฅผ ํตํด ์ ๊ทผํ๋ ์ต๊ด์ ๋ค์ด์ //์๋ฐ์์ ์ ๊ณต proxy // loader : ์ค์ ๋ก๋ํ ๊ฐ์ฒด , // interfaces : ํต์ฌ๋ก์ง์ด ๊ตฌํํ ์ธํฐํ์ด์ค. ํต์ฌ๋ก์ง์ด ์ฌ๋ฌ๊ฐ์ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ณ ์์ ์ ์๊ธฐ ๋๋ฌธ์ ๋ฐฐ์ด๋ก ์ ์ธํด์ค๋ค. // h : ๋ถ๊ฐ๊ธฐ๋ฅ์ ๊ฝ์ ์ ์๋ ๋ถ๋ถ Exam proxyExam = (Exam) Proxy.newProxyInstance(Exam.class.getClassLoader(), new Class[] {Exam.class}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // ๋ถ๊ฐ๊ธฐ๋ฅ long start = System.currentTimeMillis(); // ํต์ฌ๊ธฐ๋ฅ ํธ์ถ Object result = method.invoke(exam, args); // args : exam ์์ ๊ตฌํํ๊ณ ์๋ ๋ฉ์๋๋ค์ ํ๋ผ๋ฏธํฐ๋ก ๊ฐ์ ธ์ด // ๋ถ๊ฐ๊ธฐ๋ฅ long end = System.currentTimeMillis(); String message = (end-start) + "ms ์๊ฐ ์์ "; System.out.println(message); return result; } } ); System.out.println("total is "+ proxyExam.avg()); }
}```
์ถ๋ ฅ
1. Exam์ธํฐํ์ด์ค ์ ๊ณต 2. ํ๋ก์ํฉํ ๋ฆฌ์๊ฒ ๋์ด๋ดํฝ ํ๋ก์ ๋ง๋ค์ด๋ฌ๋ผ๊ณ ์์ฒญ 3. Exam์ธํฐํ์ด์ค์ ๋ชจ๋ ๋ฉ์๋๋ฅผ ๊ตฌํํ ์ค๋ธ์ ํธ ์์ฑ 4. InvokationHandler ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ์ค๋ธ์ ํธ๋ฅผ ์ ๊ณต โ ๋ค์ด๋ด๋ฏน ํ๋ก์๊ฐ ๋ฐ์ ๋ชจ๋ ์์ฒญ์ InvokationHandler ์ invoke() ๋ฉ์๋๋ก ๋ณด๋ด์ค. 5. Exam์ธํฐํ์ด์ค์ ๋ฉ์๋๊ฐ ์๋ฌด๋ฆฌ ๋ง์๋ invoke ๋ฉ์๋ ํ๋๋ก ์ฒ๋ฆฌํ ์ ์๋ค.
๐ฟ Spring AOP
์คํ๋ง์ ํ๋ก์ ๊ธฐ๋ฐ์ AOP ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. ๋ฐํ์ ์์ ์ ํ๊ฒ ํด๋์ค์ ๋ํ ๋ถ๊ฐ๊ธฐ๋ฅ์ ์คํํด์ค ํ๋ก์ ๋น์ ์์ฑํด์ค๋ค. (์๋ ํ๋ก์ ์์ฑ๊ธฐ)
์ฉ์ด
Aspect : ๊ณตํต์ฝ๋๋ฅผ ๋ชจ๋ํ ํ ๊ฒ
Target : Aspect ๊ฐ ์ ์ฉ๋๋ ๊ณณ
Advice : ์์ํ๊ฒ ๋ถ๊ฐ๊ธฐ๋ฅ๋ง ์ค์ ๋ก ๊ตฌํ๋ ๋ถ๋ถ. ๋ถ๊ฐ๊ธฐ๋ฅ ๊ตฌํ์ฒด
TEXT- Before- After returnning- After throwing : ์์ธ๊ฐ ๋ฐ์๋ ํ ๋์- Around : ๋ฉ์๋์ ์คํ ์์ฒด๋ฅผ ์ ์ดํ ์ ์๋ค.Joint point : Advice ๊ฐ Target์ ์ ์ฉ๋๋ ์์
Point cut : Joint point ์ ์์ธ์คํ์ ์ ์ํ ๊ฒ
TEXT- execution(@execution) : ๋ฉ์๋๋ฅผ ๊ธฐ์ค์ผ๋ก Pointcut์ ์ค์ - within(@within) : ํน์ ํ ํ์ (ํด๋์ค)์ ๊ธฐ์ค์ผ๋ก Pointcut์ ์ค์ - this : ์ฃผ์ด์ง ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๊ฐ์ฒด๋ฅผ ๋์์ผ๋ก Pointcut์ ์ค์ - args(@args) : ํน์ ํ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฐ์ง๋ ๋์๋ค๋ง์ PointCut์ผ๋ก ์ค์ - @annotation : ํน์ ํ ์ด๋ ธํ ์ด์ ์ด ์ ์ฉ๋ ๋์๋ค๋ง์ Pointcut์ผ๋ก ์ค์
์ฐธ์กฐ
- ์ ํ๋ธ newlecture ์คํ๋ง ๊ฐ์
- ํ ๋น์ ์คํ๋ง