[ 이전 글 ]Aspect Oriented Programming ( 2 )
Aspect Oriented Programming ( 2 )
[ 이전 글 ] Aspect Oriented Programming ( 1 ) Aspect Oriented Programming ( 1 ) Aspect Oriented Programming 관점 지향 프로그래밍 ( 방법론 ) OOP 사용자가 요구한 업무내용(요청)을 분석하고 이를 기반의 로직을 만듬 개
cloakinghost.tistory.com
Spring Version
보조업무의 4종류
- Before Advice ( 앞쪽 )
- After returnning Advice ( 뒤쪽 )
- After throwing Advice ( 예외처리 )
- Around Advice ( 앞, 뒤 )
이전 글에서 보인 형태가 Around Advice이다
Spring으로 바꾸자
XML
Proxy객체에 주업무를 실행할 객체와
보조업무를 실행할 객체를 지시한다
<?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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<bean id="target" class="spring.aop.entity.ScoreExam" p:kor="1" p:eng="1" p:math="1" p:com="1"/>
<bean id="logAroundAdvice" class="spring.aop.advice.LogAroundAdvice"/>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="target"/>
<property name="interceptorNames">
<list>
<value>logAroundAdvice</value>
</list>
</property>
</bean>
</beans>
실행부
아래 주석된 부분으로 실행시 Proxy 실행( 주업무 + 보조업무 )
현 상태에서는 주업무만 실행된다
package spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.aop.entity.Exam;
public class Program {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring/aop/setting.xml");
Exam exam =
// (Exam) context.getBean("target");
(Exam) context.getBean("proxy");
System.out.printf("total is %d\n", exam.total());
System.out.printf("avg is %.2f\n", exam.avg());
}
}
Proxy
Java Project에서는 invoke()
Spring에서는 proceed()
package spring.aop.advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class LogAroundAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
long start = System.currentTimeMillis();
Object result = invocation.proceed();
long end = System.currentTimeMillis();
String message = (end - start) + "ms 시간이 걸렸습니다.";
System.out.println(message);
return result;
}
}
interface
package spring.aop.entity;
public interface Exam {
int total();
float avg();
}
구현 클래스
package spring.aop.entity;
public class ScoreExam implements Exam {
private int kor;
private int eng;
private int math;
private int com;
public ScoreExam() { }
public ScoreExam(int kor, int eng, int math, int com) {
this.kor = kor;
this.eng = eng;
this.math = math;
this.com = com;
}
public void setKor(int kor) {this.kor = kor;}
public void setEng(int eng) {this.eng = eng;}
public void setMath(int math) {this.math = math;}
public void setCom(int com) {this.com = com;}
@Override
public int total() {
int result = kor + eng + math + com;
try {
// 쓰레드를 잠시 정시시켜 강제로 작업시간을 늘렸다
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
@Override
public float avg() {
float result = total() / 4.0f;
return result;
}
@Override
public String toString() {
return "ScoreExam [kor=" + kor + ", eng=" + eng +
", math=" + math + ", com=" + com + "]";
}
}
728x90
'Spring' 카테고리의 다른 글
After Returning • Throwing Advice (0) | 2023.08.07 |
---|---|
BeforeAdvice (0) | 2023.08.07 |
Around Advice ( ver. Java Project ) (0) | 2023.08.07 |
Aspect Oriented Programming (0) | 2023.08.07 |
Java Configuration (0) | 2023.08.07 |