2014년 8월 2일 토요일

[스프링프레임워크3/4,Spring AOP예제, StaticMethodMatcherPointcut,ProxyFactory를 이용,프로그래밍적인방법으로 AOP구현],오라클자바커뮤니티 개발자실무교육

[스프링프레임워크3/4,Spring AOP예제, StaticMethodMatcherPointcut,ProxyFactory를 이용,프로그래밍적인방법으로 AOP구현],오라클자바커뮤니티 개발자실무교육

이전에 작성한 프로그래밍적 방법을 통한 SmallMart AOP 예제에 getProducts2() 메소드를 추가로 만들고 StaticMethodMatcherPointcut을 적용하여 getProducts2 메소드에만 충고가 적용되도록 변경하자.


1. SmallMartInterface.java

package oraclejava.traing.aop2;

public interface SmallMartInterface {
public String getProducts(String productName) throws Exception ;
public String getProducts2(String productName) throws Exception ;
}


2. SmallMart.java

package oraclejava.traing.aop2;

public class SmallMart implements SmallMartInterface {
public String getProducts(String productName) throws Exception { 
System.out.println("getProduct()…" + productName);
//throw new Exception("예외 발생 시킴...");
return "SmallMart  getProducts......................";
}
public String getProducts2(String productName) throws Exception { 
System.out.println("getProduct2()…" + productName);
//throw new Exception("예외 발생 시킴...");
return "SmallMart  getProducts2().....................";
}
}



3. 이번에는 포인트컷을 만들자. StaticMethodMatcherPointcut은 클래스이름 or 메소드 이름과 같은 정적인 정보에 기초하여 출고를 받을 메소드를 정의하는 포인트컷이며 스프링에서 제공한다. 이를 상속받아 포인트컷 클래스를 작성하자.

SmallMart클래스의 getProducts2 메소드가 충고를 받을 메소드 이다.

package oraclejava.traing.aop2;

import java.lang.reflect.Method;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.StaticMethodMatcherPointcut;

public class SmallMartStaticPointcut extends StaticMethodMatcherPointcut{
public boolean matches(Method method, Class<?> cls) {
return ("getProducts2".equals(method.getName()));
}

public ClassFilter getClassFilter() {
return new ClassFilter() {
public boolean matches(Class<?> cls) {
return(cls== SmallMart.class);}};}
}



4. 이번에는 충고 클래스를 각각 만들자.


package oraclejava.traing.aop2;

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.traing.aop2;

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 "이건 주변충고에서 원래 getProducts 메소드의 리턴을 대체 한것입니다.";
}// :
}




package oraclejava.traing.aop2;

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 productName) … 
String findName= (String)arg1[0]; 
System.out.println(arg0.getName() + ":: 사전충고."); 
}
}





package oraclejava.traing.aop2;

import org.springframework.aop.ThrowsAdvice;

public class ThrowsLoggingAdvice implements ThrowsAdvice{
public void afterThrowing(Throwable throwable) {
System.out.println("예외충고 ....");
}
}



5. 메인클래스


package oraclejava.traing.aop2;

import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;

public class SmallMartApp {
public static void main(String[] args) {
SmallMartInterface target = new SmallMart();
Pointcut pc = new SmallMartStaticPointcut();
Advice advice1= new AfterLoggingAdvice();
Advisor advisor1= new DefaultPointcutAdvisor(pc, advice1);
Advice advice2= new BeforeLoggingAdvice();
Advisor advisor2= new DefaultPointcutAdvisor(pc, advice2);
Advice advice3= new AroundLoggingAdvice();
Advisor advisor3= new DefaultPointcutAdvisor(pc, advice3);
Advice advice4= new ThrowsLoggingAdvice();
Advisor advisor4= new DefaultPointcutAdvisor(pc, advice4);
ProxyFactory pf = new ProxyFactory();
pf.addAdvisor(advisor1);
pf.addAdvisor(advisor2);
pf.addAdvisor(advisor3);
pf.addAdvisor(advisor4);
pf.setTarget(target);
try {
SmallMartInterface proxy = (SmallMartInterface) pf.getProxy();
System.out.println(proxy.getProducts("오라클자바커뮤니티"));
System.out.println(proxy.getProducts2("오라클자바커뮤니티"));
} catch (Exception e) {
e.printStackTrace(System.out);
}
}

}



[결과]


getProduct()…생필품
SmallMart  getProducts......................
getProducts2:: 사전충고.
메소드실행..
getProduct2()…생필품
메소드실행후
getProducts2 : 사후충고
이건 주변충고에서 원래 getProducts 메소드의 리턴을 대체 한것입니다.

댓글 없음:

댓글 쓰기