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 exam = (Exam)context.getBean("exam");
System.out.printf("total is %d\n",exam.total());
System.out.printf("avg is %f\n",exam.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="logBeforeAdvice" class="spring.aop.advice.LogBeforeAdvice" />
<bean id="exam" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="target"/>
<property name="interceptorNames">
<list>
<value>logAroundAdvice</value>
<value>logBeforeAdvice</value>
</list>
</property>
</bean>
</beans>
package spring.aop.advice;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class LogBeforeAdvice implements MethodBeforeAdvice{
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
// TODO Auto-generated method stub
System.out.println("앞에서 실행될 로직");
}
}
https://www.youtube.com/watch?v=oysjZMw9S6M&list=PLq8wAnVUcTFUHYMzoV2RoFoY2HDTKru3T&index=22
'Spring' 카테고리의 다른 글
스프링 Part2 AOP 강좌 07강 _ Point Cut(Weaving, Join Point) (0) | 2022.01.02 |
---|---|
스프링 Part2 AOP 강좌 06강 _ After Returning / Throwing 어드바이스 구현하기 (0) | 2022.01.02 |
스프링 Part2 AOP 강좌 04강 _ 스프링으로 AOP 구현해보기-AroundAdvice (0) | 2022.01.02 |
스프링 Part2 AOP 강좌 03강 - 순수 자바로 AOP 구현해보기 (2) | 2022.01.02 |
스프링 Part2 AOP 강좌 02강 _ AOP 자바 코드 이해하기 (0) | 2021.12.28 |
댓글