From 267e4da3f4948cace98506aa847a780d03969960 Mon Sep 17 00:00:00 2001 From: taesan Date: Tue, 31 Dec 2019 18:15:15 +0900 Subject: [PATCH] =?UTF-8?q?AOP=EA=B0=9C=EB=85=90=20=EC=9E=A1=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring/aop/Program.java | 23 ++++++ .../aop/advice/LogAfterReturningAdvice.java | 14 ++++ spring/aop/advice/LogAfterThrowingAdvice.java | 10 +++ spring/aop/advice/LogAroundAdvice.java | 29 +++++++ spring/aop/advice/LogBeforeAdvice.java | 14 ++++ spring/aop/entity/Exam.java | 6 ++ spring/aop/entity/NewlecExam.java | 75 +++++++++++++++++++ spring/aop/setting.xml | 63 ++++++++++++++++ 8 files changed, 234 insertions(+) create mode 100644 spring/aop/Program.java create mode 100644 spring/aop/advice/LogAfterReturningAdvice.java create mode 100644 spring/aop/advice/LogAfterThrowingAdvice.java create mode 100644 spring/aop/advice/LogAroundAdvice.java create mode 100644 spring/aop/advice/LogBeforeAdvice.java create mode 100644 spring/aop/entity/Exam.java create mode 100644 spring/aop/entity/NewlecExam.java create mode 100644 spring/aop/setting.xml diff --git a/spring/aop/Program.java b/spring/aop/Program.java new file mode 100644 index 0000000..4c61b31 --- /dev/null +++ b/spring/aop/Program.java @@ -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 diff --git a/spring/aop/advice/LogAfterReturningAdvice.java b/spring/aop/advice/LogAfterReturningAdvice.java new file mode 100644 index 0000000..51d6503 --- /dev/null +++ b/spring/aop/advice/LogAfterReturningAdvice.java @@ -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()); + } +} diff --git a/spring/aop/advice/LogAfterThrowingAdvice.java b/spring/aop/advice/LogAfterThrowingAdvice.java new file mode 100644 index 0000000..323435c --- /dev/null +++ b/spring/aop/advice/LogAfterThrowingAdvice.java @@ -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()); + } +} diff --git a/spring/aop/advice/LogAroundAdvice.java b/spring/aop/advice/LogAroundAdvice.java new file mode 100644 index 0000000..06b9771 --- /dev/null +++ b/spring/aop/advice/LogAroundAdvice.java @@ -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; + } +} diff --git a/spring/aop/advice/LogBeforeAdvice.java b/spring/aop/advice/LogBeforeAdvice.java new file mode 100644 index 0000000..0b283da --- /dev/null +++ b/spring/aop/advice/LogBeforeAdvice.java @@ -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() + "] 앞에서 실행될 로직 "); + } +} diff --git a/spring/aop/entity/Exam.java b/spring/aop/entity/Exam.java new file mode 100644 index 0000000..ff1f899 --- /dev/null +++ b/spring/aop/entity/Exam.java @@ -0,0 +1,6 @@ +package spring.aop.entity; + +public interface Exam { + int total(); + float avg(); +} diff --git a/spring/aop/entity/NewlecExam.java b/spring/aop/entity/NewlecExam.java new file mode 100644 index 0000000..ff8bc85 --- /dev/null +++ b/spring/aop/entity/NewlecExam.java @@ -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; + } + +} diff --git a/spring/aop/setting.xml b/spring/aop/setting.xml new file mode 100644 index 0000000..943bf2c --- /dev/null +++ b/spring/aop/setting.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + .*to.* + + + + + + + + + + + + + + + + + + + + classicAroundAdvisor + classicBeforeAdvisor + logAfterReturningAdvice + logAfterThrowingAdvice + + + + +