Spring aop
Namespace를 이용한 AOP구현(Spring 선언적 AOP, aop 네임스페이스)-SmallMart예제
이전에 작성한 SmallMart예제를
aop namespace를 이용한 방식으로 바꾸어 보자.
[SmallMart.java]
public interface SmallMart {
public
void getProducts(String productName) throws Exception;
}
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
[SmallMartImpl.java]
public class SmallMartImpl implements
SmallMart {
public void getProducts(String
productName) throws Exception {
System.out.println("getProduct()..."+productName);
}
}
[OracleJavaAdvice.java]
import
org.aspectj.lang.JoinPoint;
import
org.aspectj.lang.ProceedingJoinPoint;
public
class OracleJavaAdvice {
public
void afterReturning(JoinPoint joinPoint) {
// TODO
Auto-generated method stub
System.out.println(joinPoint.getSignature().getName() +" :
사후충고");
}
public
void before(JoinPoint joinPoint) {
// TODO
Auto-generated method stub
System.out.println(joinPoint.getSignature().getName()+"::사전충고.");
}
public
void afterThrowing(){
System.out.println("예외충고 발생...");
}
public
Object invoke(ProceedingJoinPoint joinPoint) throws Throwable
{
// TODO
Auto-generated method stub
String
findName = (String)joinPoint.getSignature().getName();
if
(findName == null){
throw new
Exception("예외");
}
System.out.println("메소드 실행전");
Object
returnedObj = joinPoint.proceed();
System.out.println("메소드 실행후");
return
returnedObj;
}
}
[SmallMartApp.java]
import
org.springframework.context.support.GenericXmlApplicationContext;
public class SmallMartApp {
public static void main(String[]
args) throws Exception{
// TODO Auto-generated method
stub
GenericXmlApplicationContext ctx = new
GenericXmlApplicationContext();
ctx.load("small.xml");
ctx.refresh();
SmallMart smallMart =
(SmallMart)ctx.getBean("smallMart");
smallMart.getProducts("과자");
ctx.close();
}
}
[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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop-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">
<aop:config>
<aop:pointcut __EXPRESSION__="execution(*
onj.edu.small..getProducts*(String)) and bean(smallMart*)"
id="onjpointcut"/>
<aop:aspect ref="advice">
<aop:before method="before"
pointcut-ref="onjpointcut"/>
<aop:after-returning method="afterReturning"
pointcut-ref="onjpointcut"/>
<aop:around method="invoke"
pointcut-ref="onjpointcut"/>
<aop:after-throwing method="afterThrowing"
pointcut-ref="onjpointcut"/>
</aop:aspect>
</aop:config>
<bean
id="advice" class="onj.edu.small.OracleJavaAdvice"/>
<bean
id="smallMart" class="onj.edu.small.SmallMartImpl"/>
</beans>
댓글 없음:
댓글 쓰기