AOP개념 잡기
This commit is contained in:
23
spring/aop/Program.java
Normal file
23
spring/aop/Program.java
Normal 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
|
||||
14
spring/aop/advice/LogAfterReturningAdvice.java
Normal file
14
spring/aop/advice/LogAfterReturningAdvice.java
Normal 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());
|
||||
}
|
||||
}
|
||||
10
spring/aop/advice/LogAfterThrowingAdvice.java
Normal file
10
spring/aop/advice/LogAfterThrowingAdvice.java
Normal 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());
|
||||
}
|
||||
}
|
||||
29
spring/aop/advice/LogAroundAdvice.java
Normal file
29
spring/aop/advice/LogAroundAdvice.java
Normal 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;
|
||||
}
|
||||
}
|
||||
14
spring/aop/advice/LogBeforeAdvice.java
Normal file
14
spring/aop/advice/LogBeforeAdvice.java
Normal 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() + "] 앞에서 실행될 로직 ");
|
||||
}
|
||||
}
|
||||
6
spring/aop/entity/Exam.java
Normal file
6
spring/aop/entity/Exam.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package spring.aop.entity;
|
||||
|
||||
public interface Exam {
|
||||
int total();
|
||||
float avg();
|
||||
}
|
||||
75
spring/aop/entity/NewlecExam.java
Normal file
75
spring/aop/entity/NewlecExam.java
Normal 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
63
spring/aop/setting.xml
Normal 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>
|
||||
Reference in New Issue
Block a user