Exam target = (Exam)context.getBean("target");
변수명을
proxy로 두면 주업무+보조업무를 실행하게 되고
target으로 두고 실행하면 주업무만 실행하게 된다.
package spring.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.aop.entity.Exam;
import spring.aop.entity.NewlecExam;
import spring.di.NewlecDiConfig;
public class Program {
public static void main(String[] args) {
ApplicationContext context
//=new AnnotationConfigApplicationContext(NewlecDiConfig.class);
= new ClassPathXmlApplicationContext("spring/aop/setting.xml");
Exam proxy = (Exam)context.getBean("proxy");
System.out.printf("total is %d\n",proxy.total());
System.out.printf("avg is %f\n",proxy.avg());
}
}
<?xml version="1.0" encoding="UTF-8"?>
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.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.NewlecExam"
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>
package spring.aop.entity;
public class NewlecExam implements Exam {
private int kor;
private int eng;
private int math;
private int com;
public NewlecExam() {
// TODO Auto-generated constructor stub
}
public NewlecExam(int kor, int eng, int math, int com) {
super();
this.kor = kor;
this.eng = eng;
this.math = math;
this.com = com;
}
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;
}
public int getCom() {
return com;
}
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
@Override
public float avg() {
float result = total()/4.0f;
return result;
}
@Override
public String toString() {
return "NewlecExam [kor=" + kor + ", eng=" + eng + ", math=" + math + ", com=" + com + "]";
}
}
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;
}
}
https://www.youtube.com/watch?v=eHYPujubB3E&list=PLq8wAnVUcTFUHYMzoV2RoFoY2HDTKru3T&index=21
'Spring' 카테고리의 다른 글
스프링 Part2 AOP 강좌 06강 _ After Returning / Throwing 어드바이스 구현하기 (0) | 2022.01.02 |
---|---|
스프링 Part2 AOP 강좌 05강 _ BeforeAdvice 구현하기 (1) | 2022.01.02 |
스프링 Part2 AOP 강좌 03강 - 순수 자바로 AOP 구현해보기 (2) | 2022.01.02 |
스프링 Part2 AOP 강좌 02강 _ AOP 자바 코드 이해하기 (0) | 2021.12.28 |
스프링 Part2 AOP 강좌 01강 - AOP(Aspect Oriented Programming) 이란? (0) | 2021.12.28 |
댓글