AOP(Aspect Oriented Programing)
- ์์์ ํตํ ๋ฐฉ๋ฒ์ ํ๊ณ๊ฐ ์์ด์ ๋ฑ์ฅ, ํต์ฌ๊ธฐ๋ฅ๊ณผ ๊ณตํต ๊ธฐ๋ฅ์ ๋ถ๋ฆฌ ์์ผ๋๊ณ , ๊ณตํต ๊ธฐ๋ฅ์ ํ์๋ก ํ๋ ํต์ฌ ๊ธฐ๋ฅ๋ค์์ ์ฌ์ฉํ๋ ๋ฐฉ์
- ๊ณตํต ๊ธฐ๋ฅ์ ํต์ฌ ๊ธฐ๋ฅ๊ณผ ๋ถ๋ฆฌํด๋๊ณ , ๊ณตํต ๊ธฐ๋ฅ ์ค์์ ํต์ฌ ๊ธฐ๋ฅ์ ์ ์ฉํ๊ณ ์ ํ๋ ๋ถ๋ถ์ ์ ์ฉํ๋ ๊ฒ
- ํธ๋์ญ์
, ๋ก๊น
, ๋ณด์๊ณผ ๊ฐ์ ์ํฐํ๋ผ์ด์ฆ ์๋น์ค๋ฅผ ์ ์ฉํ๊ณ ๊น๋ํ ์ฝ๋๋ฅผ ์ ์ง
- Aspect : ๊ณตํต ๊ธฐ๋ฅ
- Advice : Aspect์ ๊ธฐ๋ฅ ์์ฒด
- Joinpoint : Advice๋ฅผ ์ ์ฉํด์ผ ๋๋ ๋ถ๋ถ(ex.ํ๋, ๋ฉ์๋)
- Pointcut : Joinpoint์ ๋ถ๋ถ์ผ๋ก ์ค์ ๋ก Advice๊ฐ ์ ์ฉ๋ ๋ถ๋ถ
- Weaving : Advice๋ฅผ ํต์ฌ ๊ธฐ๋ฅ์ ์ ์ฉํ๋ ํ์
-
aop:around : ๋ฉ์๋ ์คํ ์ /ํ ๋ฐ exception ๋ฐ์์ advice ์คํ
-
aop:before : ๋ฉ์๋ ์คํ ์ ์ advice ์คํ
-
aop:after-returning : ์ ์์ ์ผ๋ก ๋ฉ์๋ ์คํ ํ์ advice ์คํ
-
aop:after-throwing : ๋ฉ์๋ ์คํ ์ค exception ๋ฐ์์ advice ์คํ
-
aop:after : ๋ฉ์๋ ์คํ ์ค exception์ด ๋ฐ์ํ์ฌ๋ advice ์คํ
@Aspect๋ฅผ ์ด์ฉํ AOP๊ตฌํ
- ์์กด ์ค์ (pom.xml)
- @Aspect ์ด๋
ธํ
์ด์
์ ์ด์ฉํ Aspectํด๋์ค ์ ์
- XMLํ์ผ์ aop:aspectj-autoproxy/ ์ค์
AspectJ Pointcut ํํ์
- @Pointcut("execution(public void get*(..))")
- @Pointcut("within(com.javalec.ex.*)")
- @Pointcut("bean(student)")
package com.javalec.ex;
public class Student {
private String name;
private int age;
private int gradeNum;
private int classNum;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGradeNum() {
return gradeNum;
}
public void setGradeNum(int gradeNum) {
this.gradeNum = gradeNum;
}
public int getClassNum() {
return classNum;
}
public void setClassNum(int classNum) {
this.classNum = classNum;
}
public void getStudentInfo() {
System.out.println("์ด๋ฆ : " + getName());
System.out.println("๋์ด : " + getAge());
System.out.println("ํ๋
: " + getGradeNum());
System.out.println("๋ฐ : " + getClassNum());
}
}
package com.javalec.ex;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LogAop {
@Pointcut("within(com.javalec.ex.*)")
private void pointcutMethod() {
}
@Around("pointcutMethod()")
public Object loggerAop(ProceedingJoinPoint joinpoint) throws Throwable {
String signatureStr = joinpoint.getSignature().toShortString();
System.out.println(signatureStr + "is start");
long st = System.currentTimeMillis();
try {
Object obj = joinpoint.proceed();
return obj;
} finally {
long et = System.currentTimeMillis();
System.out.println(signatureStr + "is finished");
System.out.println(signatureStr + "๊ฒฝ๊ณผ์๊ฐ" + (et-st));
}
}
@Before("within(com.javalec.ex.*)")
public void beforeAdvice() {
System.out.println("before");
}
}
<?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"
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">
<aop:aspectj-autoproxy/>
<bean id="logAop" class="com.javalec.ex.LogAop"/>
<bean id="student" class="com.javalec.ex.Student">
<property name="name" value="ํ๊ธธ๋"/>
<property name="age" value="10" />
<property name="gradeNum" value="3"/>
<property name="classNum" value="5"/>
</bean>
</beans>
package com.javalec.ex;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
AbstractApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationCTX.xml");
Student student = ctx.getBean("student", Student.class);
student.getStudentInfo();
ctx.close();
}
}