[구로디지털단지/가산역,Spring AOP예제, StaticMethodMatcherPointcut,ProxyFactory를 이용,프로그래밍적인방법으로 AOP구현, 스프링AOP교육학원,Spring Framework학원교육센터]
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 메소드의 리턴을 대체 한것입니다.
평일주간[100%환급과정]
(8/25)C#4.0,WinForm,ADO.NET(8/25)안드로이드개발자과정
(8/25)SQL기초에서 Schema Object까지
(8/29)Spring,MyBatis,Hibernate실무과정
(8/29)자바기초JDBC,Servlet/JSP까지
(8/29)PL/SQL,ORACLE HINT,TUNING
(8/25)오라클자바채용확정교육
평일야간[개인80%환급]
(8/21)Spring, MyBatis, Hibernate
(8/21)HTML5,CSS3,Ajax,jQuery마스터
(8/21)C#,Network,ADO.NET,ASP.NET
(8/26)SQL기초에서실무까지
(8/26)안드로이드개발자과정
(8/28)자바JSP,jQuery,Spring,MyBatis
주말주간[개인80%환급]
(8/23)자바웹&스프링,마이바티스
(8/23)Spring, MyBatis, Hibernate
(8/23)SQL기초에서실무까지
(8/23)자바,네트워크,웹&스프링
(8/30)안드로이드개발자과정
(8/30)C#,ASP.NET마스터(8/30)웹퍼블리싱 마스터
주말야간[개인80%환급]
(8/23)SQL기초에서실무까지
(8/23)자바,네트워크,웹&스프링
댓글 없음:
댓글 쓰기