[Spring AOP예제,ProxyFactoryBean을 이용한 Spring Framework AOP구현예제, 사전/사후/주변/예외충고]
프로그래밍적인 방법을 통해 구현한 이전 SmallMart 예제를 ProxyFactoryBean을 이용하여 구현해 보자.
이전 예제는 다음 URL에서 확인하세요.
http://oraclejavacommunity.co.kr/bbs/board.php?bo_table=LecSpring&wr_id=285&sca=&sfl=wr_subject%7C%7Cwr_content&stx=SmallMart&sop=and
package oraclejava.training.aop;
public interface SmallMartInterface {
public void
getProducts(String productName) throws Exception;
}
package oraclejava.training.aop;
public class SmallMart implements SmallMartInterface{
public void
getProducts(String productName) throws Exception{
System.out.println("getProduct()..." +
productName);
throw new
Exception("error");
}
}
package oraclejava.training.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeLoggingAdvice implements MethodBeforeAdvice{
public void
before(Method arg0, Object[] arg1, Object arg2)
throws
Throwable{
String findName
= (String)arg1[0];
System.out.println(arg0.getName() + ";;사전충고");
}
}
package oraclejava.training.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterLoggingAdvice implements AfterReturningAdvice{
public void
afterReturning(Object arg0, Method arg1, Object[] arg2,Object arg3)
throws
Throwable{System.out.println(arg1.getName() + ": 사후충고");
}
}
package oraclejava.training.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class AroundLoggingAdvice implements MethodInterceptor{
public Object
invoke(MethodInvocation arg0) throws Throwable{
String findName=
(String)arg0.getArguments()[0];
if(findName==
null) {
throw new
Throwable("예외");
}
System.out.println("메소드실행전");
Object
returnedObj= arg0.proceed();
System.out.println("메소드실행후");
return
returnedObj;
}
}
package oraclejava.training.aop;
import org.springframework.aop.ThrowsAdvice;
public class ThrowsLoggingAdvice implements ThrowsAdvice{
public void
afterThrowing(Throwable throwable){
System.out.println("예외충고 발생...");
}
}
package oraclejava.training.aop;
import org.springframework.aop.framework.ProxyFactory;
import
org.springframework.context.support.GenericXmlApplicationContext;
public class SmallMartApp {
public static
void main(String[] args) throws Exception{
GenericXmlApplicationContext ctx = new
GenericXmlApplicationContext();
ctx.load("smallmart.xml");
ctx.refresh();
SmallMartInterface smallMart =
(SmallMartInterface)ctx.getBean("smallMartProxy");
smallMart.getProducts("과자");
ctx.close();
}
}
(/src/main/resources아래) smallmart.xml
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<bean
id="smallMartProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property
name="target">
<ref
local="smallMart"/>
</property>
<property
name="interceptorNames">
<list>
<value>beforeAdvice</value>
<value>afterAdvice</value>
<value>aroundAdvice</value>
<value>throwsAdvice</value>
</list>
</property>
</bean>
<bean
id="beforeAdvice" class="oraclejava.training.aop.BeforeLoggingAdvice"/>
<bean
id="afterAdvice" class="oraclejava.training.aop.AfterLoggingAdvice"/>
<bean
id="aroundAdvice" class="oraclejava.training.aop.AroundLoggingAdvice"/>
<bean
id="throwsAdvice" class="oraclejava.training.aop.ThrowsLoggingAdvice"/>
<bean
id="smallMart" class="oraclejava.training.aop.SmallMart"/>
</beans>
[결과]
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in
[jar:file:/C:/java/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in
[jar:file:/C:/Documents%20and%20Settings/Administrator/.m2/repository/ch/qos/logback/logback-classic/1.0.13/logback-classic-1.0.13.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an
explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader -
Loading XML bean definitions from class path resource [smallmart.xml]
INFO : org.springframework.context.support.GenericXmlApplicationContext -
Refreshing
org.springframework.context.support.GenericXmlApplicationContext@9036e: startup
date [Sun Jun 01 17:42:32 KST 2014]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory
- Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@14a4067:
defining beans
[smallMartProxy,beforeAdvice,afterAdvice,aroundAdvice,throwsAdvice,smallMart];
root of factory hierarchy
getProducts;;사전충고
메소드실행전
getProduct()...과자
예외충고 발생...
Exception in thread "main" java.lang.Exception: error
at
oraclejava.training.aop.SmallMart.getProducts(SmallMart.java:6)
at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at
java.lang.reflect.Method.invoke(Unknown Source)
at
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at
org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:124)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at
oraclejava.training.aop.AroundLoggingAdvice.invoke(AroundLoggingAdvice.java:16)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at
org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:51)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at
org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:51)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at
com.sun.proxy.$Proxy0.getProducts(Unknown Source)
at
oraclejava.training.aop.SmallMartApp.main(SmallMartApp.java:15)
댓글 없음:
댓글 쓰기