AOP개념 잡기

This commit is contained in:
taesan
2019-12-31 18:15:15 +09:00
parent 668a0ed7de
commit 267e4da3f4
8 changed files with 234 additions and 0 deletions

23
spring/aop/Program.java Normal file
View File

@@ -0,0 +1,23 @@
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");
//new AnnotationConfigApplicationContext(ExamAppConfig.class);
Exam exam = (Exam)context.getBean("exam");
System.out.println("##### proxy #####");
System.out.printf("total is %d\n", exam.total());
System.out.printf("avg is %f\n", exam.avg());
}//main
}//class

View File

@@ -0,0 +1,14 @@
package spring.aop.advice;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
public class LogAfterReturningAdvice implements AfterReturningAdvice{
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("returnValue : " + returnValue + ", method : " + method.getName());
}
}

View File

@@ -0,0 +1,10 @@
package spring.aop.advice;
import org.springframework.aop.ThrowsAdvice;
public class LogAfterThrowingAdvice implements ThrowsAdvice{
public void afterThrowing(IllegalArgumentException e) throws Throwable{
System.out.println("예외가 발생하였습니다. : " + e.getMessage());
}
}

View File

@@ -0,0 +1,29 @@
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();
//앞에서 했던 invoke메서드와 동일
Object result = invocation.proceed();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
String message = (end-start)+"ms 시간이 걸렸습니다.";
System.out.println(message);
return result;
}
}

View File

@@ -0,0 +1,14 @@
package spring.aop.advice;
import java.lang.reflect.Method;
import org.aopalliance.aop.Advice;
import org.springframework.aop.MethodBeforeAdvice;
public class LogBeforeAdvice implements MethodBeforeAdvice{
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("[ "+this.getClass() + "] 앞에서 실행될 로직 ");
}
}

View File

@@ -0,0 +1,6 @@
package spring.aop.entity;
public interface Exam {
int total();
float avg();
}

View File

@@ -0,0 +1,75 @@
package spring.aop.entity;
public class NewlecExam implements Exam {
private int kor;
private int eng;
private int math;
private int com;
public NewlecExam() {
}
public NewlecExam(int kor, int eng, int math, int com) {
this.kor=kor;
this.eng=eng;
this.math=math;
this.com=com;
}
@Override
public int total() {
int result = kor+eng+math+com;
if ( kor > 100 || eng > 100 || math > 100 || com > 100 )
throw new IllegalArgumentException(" 점수는 100점을 넘을 수 없습니다.");
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 + "]";
}
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;
}
}

63
spring/aop/setting.xml Normal file
View File

@@ -0,0 +1,63 @@
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!--
<bean id="classicPointCut" class="org.springframework.aop.support.NameMatchMethodPointcut">
<property name="mappedName" value="total" />
</bean>
<bean id="classicBeforeAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="logBeforeAdvice"/>
<property name="pointcut" ref="classicPointCut" />
</bean>
-->
<!-- <bean id="classicBeforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> -->
<!-- <property name="advice" ref="logBeforeAdvice" /> -->
<!-- <property name="mappedNames"> -->
<!-- <list> -->
<!-- <value>total</value> -->
<!-- <value>avg</value> -->
<!-- </list> -->
<!-- </property> -->
<!-- </bean> -->
<bean id="classicBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="logBeforeAdvice" />
<property name="patterns">
<list>
<value>.*to.*</value>
</list>
</property>
</bean>
<bean id="classicAroundAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="logAroundAdvice" />
<property name="mappedName" value="total" />
</bean>
<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="logAfterReturningAdvice" class="spring.aop.advice.LogAfterReturningAdvice" />
<bean id="logAfterThrowingAdvice" class="spring.aop.advice.LogAfterThrowingAdvice" />
<bean id="exam" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="target" />
<property name="interceptorNames">
<list>
<value>classicAroundAdvisor</value>
<value>classicBeforeAdvisor</value>
<value>logAfterReturningAdvice</value>
<value>logAfterThrowingAdvice</value>
</list>
</property>
</bean>
</beans>