2014년 7월 26일 토요일

[오라클자바커뮤니티,스프링교육학원추천.Spring Framework교육은 여기에서,.ProxyFactoryBean을 이용한 Spring Framework AOP구현예제, Pointcut 사용, 사전/사후/주변/예외충고]

[오라클자바커뮤니티,스프링교육학원추천.Spring Framework교육은 여기에서,.ProxyFactoryBean을 이용한 Spring Framework AOP구현예제, Pointcut 사용, 사전/사후/주변/예외충고]

프로그래밍적인 방법을 통해 구현한 이전 SmallMart 예제를 ProxyFactoryBean을 이용하여 구현해 보자. (Pointcut을 사용하여 getProducts 메소드만 충고를 받을 수 있도록 구현)

이전 예제는 다음 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>smallMartAdvisor1</value>
<value>smallMartAdvisor2</value>
<value>smallMartAdvisor3</value>
<value>smallMartAdvisor4</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="smallMartAdvisor1" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice">
<ref local="beforeAdvice"></ref>
</property>
<property name="pointcut">
   <!--  포인트컷으로 익명 빈을 사용 -->
<bean class="org.springframework.aop.aspectj.AspectJ__EXPRESSION__Pointcut">
<property name="__EXPRESSION__">
<value>execution(* getProducts*(..))</value>
</property>
</bean>
</property>
</bean>
<bean id="smallMartAdvisor2" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice">
<ref local="afterAdvice"></ref>
</property>
<property name="pointcut">
   <!--  포인트컷으로 익명 빈을 사용 -->
<bean class="org.springframework.aop.aspectj.AspectJ__EXPRESSION__Pointcut">
<property name="__EXPRESSION__">
<value>execution(* getProducts*(..))</value>
</property>
</bean>
</property>
</bean>
<bean id="smallMartAdvisor3" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice">
<ref local="aroundAdvice"></ref>
</property>
<property name="pointcut">
   <!--  포인트컷으로 익명 빈을 사용 -->
<bean class="org.springframework.aop.aspectj.AspectJ__EXPRESSION__Pointcut">
<property name="__EXPRESSION__">
<value>execution(* getProducts*(..))</value>
</property>
</bean>
</property>
</bean>
<bean id="smallMartAdvisor4" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice">
<ref local="throwsAdvice"></ref>
</property>
<property name="pointcut">
   <!--  포인트컷으로 익명 빈을 사용 -->
<bean class="org.springframework.aop.aspectj.AspectJ__EXPRESSION__Pointcut">
<property name="__EXPRESSION__">
<value>execution(* getProducts*(..))</value>
</property>
</bean>
</property>
</bean>
<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@a63599: startup date [Sun Jun 01 18:33:19 KST 2014]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@16b4f5a: defining beans [smallMartProxy,beforeAdvice,afterAdvice,aroundAdvice,throwsAdvice,smallMartAdvisor1,smallMartAdvisor2,smallMartAdvisor3,smallMartAdvisor4,smallMart]; root of factory hierarchy
getProducts;;사전충고
메소드실행전
getProduct()...과자
메소드실행후
getProducts: 사후충고
INFO : org.springframework.context.support.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@a63599: startup date [Sun Jun 01 18:33:19 KST 2014]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@16b4f5a: defining beans [smallMartProxy,beforeAdvice,afterAdvice,aroundAdvice,throwsAdvice,smallMartAdvisor1,smallMartAdvisor2,smallMartAdvisor3,smallMartAdvisor4,smallMart]; root of factory hierarchy

오라클자바커뮤니티교육센터, 개발자전문교육, 개인80%환급
www.oraclejavacommunity.com


평일주간(9:30~18:10) 개강
(7/28)[기업100%환급]안드로이드개발자과정
(8/04)[기업100%환급]C#4.0,WinForm,ADO.NET프로그래밍
(8/04)[기업100%환급]자바기초에서 JDBC, Servlet/JSP까지 
(8/04)[기업100%환급]PL/SQL,ORACLE HINT,TUNING
(8/11)[기업100%환급]SQL기초에서 Schema Object까지
(8/11)[기업100%환급]Spring ,MyBatis,Hibernate실무과정
(8/11)[채용예정교육]오라클자바개발잘하는신입뽑기프로젝트,교육전취업확정

평일야간(19:00~21:50) 개강
(7/29)안드로이드개발자과정
(7/29)Spring3.X, MyBatis, Hibernate실무과정
(8/04)웹퍼블리싱 마스터
(8/05)JSP,Ajax,jQUERY,Spring,MyBatis,Hibernate속성과정
(8/08)닷넷(C#,Network,ADO.NET,ASP.NET)마스터과정
(8/11)SQL기초에서실무까지
(8/28)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지

주말(10:00~17:50) 개강
(8/02)Spring3.X, MyBatis, Hibernate실무과정
(8/02)C#,ASP.NET마스터
(8/02)웹퍼블리싱 마스터
(8/02)SQL초보에서 Schema Object까지
(8/09)개발자를위한PLSQL,SQL튜닝,힌트
(8/09)안드로이드개발자과정
(8/09)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(8/23)JAVA,Network&WEB&Framework(자바기초에서웹스프링까지)

주말저녁(18:30~22:20) 개강
(8/02)JAVA,Network&WEB&Framework
(8/09)SQL기초에서실무까지

댓글 없음:

댓글 쓰기