2014년 5월 6일 화요일

[오라클자바커뮤니티]스프링 AOP, 프록시팩토리빈이용,ProxyFactoryBean을 이용한 선언적인 방법으로 Spring AOP 구현

[오라클자바커뮤니티]스프링 AOP, 프록시팩토리빈이용,ProxyFactoryBean을 이용한 선언적인 방법으로 Spring AOP 구현 


- ProxyFactoryBean을 이용한 선언적인 방법으로... 

-------------------------- OnjWorld.java --------------------------- 

package onj.aop.programming2; 

public interface OnjWorld { 

public void hello(); 
public void hello2() throws Exception; 



------------------OnjWorldImpl.java----------------- 

package onj.aop.programming2; 

public class OnjWorldImpl{ 

public void hello() { 
System.out.println("OnJ1에 오신 것을 환영합니다."); 



public void hello2() throws Exception { 
System.out.println("OnJ2에 오신 것을 환영합니다."); 




------------OnjWorldBean.java------------ 

package onj.aop.programming2; 

public class OnjWorldBean { 

private OnjWorldImpl onjWorldImpl; 

public void run() { 
try { 
onjWorldImpl.hello(); 
onjWorldImpl.hello2(); 

} catch(Exception e){ 

System.out.println("ERROR ::::"+ e); 



public void setOnjWorldImpl(OnjWorldImpl onjWorldImpl) { 
this.onjWorldImpl = onjWorldImpl; 



------------OnjWorldExam.java------------ 

package onj.aop.programming2; 

import org.springframework.context.support.GenericXmlApplicationContext; 

public class OnjWorldExam { 

public static void main(String[] args) { 
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); 
ctx.load("aop-onjworld.xml"); 
ctx.refresh(); 

OnjWorldBean bean1 = (OnjWorldBean)ctx.getBean("onjWorldBean1"); 

System.out.println("--------------------OnjWorld Bena Run()------------------"); 

bean1.run(); 

ctx.close(); 



--------------------aop-onjworld.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" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

<bean id="onjWorldBean1" class="onj.aop.programming2.OnjWorldBean"> 
<property name="onjWorldImpl"> 
<ref local="onjWorld1" /> 
</property> 
</bean> 

<bean id="onjWorld1" class="org.springframework.aop.framework.ProxyFactoryBean"> 
<property name="target"> 
<ref local="onjWorldImpltarget" /> 
</property> 
<property name="interceptorNames"> 
<list> 
<value>beforeAdvisor</value> 
<value>afterAdvisor</value> 
<value>aroundAdvisor</value> 
<value>throwsAdvisor</value> 
</list> 
</property> 
</bean> 

<bean id="onjWorldImpltarget" class="onj.aop.programming2.OnjWorldImpl" /> 

<bean id="beforeAdvice" class="onj.aop.programming2.BeforeLoggingAdvice" /> 
<bean id="afterAdvice" class="onj.aop.programming2.AfterLoggingAdvice" /> 
<bean id="aroundAdvice" class="onj.aop.programming2.AroundLoggingAdvice" /> 
<bean id="throwsAdvice" class="onj.aop.programming2.ThrowsLoggingAdvice" /> 

<bean id="beforeAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> 
<property name="advice"> 
<ref local="beforeAdvice"/> 
</property> 
<property name="pointcut"> 
<bean class="org.springframework.aop.aspectj.AspectJ__EXPRESSION__Pointcut"> 
<property name="__EXPRESSION__"> 
<value>execution(* hello2*(..))</value> 
</property> 
</bean> 
</property> 
</bean> 

<bean id="afterAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> 
<property name="advice"> 
<ref local="afterAdvice"/> 
</property> 
<property name="pointcut"> 
<bean class="org.springframework.aop.aspectj.AspectJ__EXPRESSION__Pointcut"> 
<property name="__EXPRESSION__"> 
<value>execution(* hello2*(..))</value> 
</property> 
</bean> 
</property> 
</bean> 

<bean id="aroundAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> 
<property name="advice"> 
<ref local="aroundAdvice"/> 
</property> 
<property name="pointcut"> 
<bean class="org.springframework.aop.aspectj.AspectJ__EXPRESSION__Pointcut"> 
<property name="__EXPRESSION__"> 
<value>execution(* hello2*(..))</value> 
</property> 
</bean> 
</property> 
</bean> 

<bean id="throwsAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> 
<property name="advice"> 
<ref local="throwsAdvice"/> 
</property> 
<property name="pointcut"> 
<bean class="org.springframework.aop.aspectj.AspectJ__EXPRESSION__Pointcut"> 
<property name="__EXPRESSION__"> 
<value>execution(* hello2*(..))</value> 
</property> 
</bean> 
</property> 
</bean> 

</beans> 




-------------------- AfterLoggingAdvice.java--------------------- 

package onj.aop.programming2; 

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() + ":: 사후충고"); 





-------------------- AroundLoggingAdvice.java --------------------- 
package onj.aop.programming2; 

import org.aopalliance.intercept.MethodInterceptor; 
import org.aopalliance.intercept.MethodInvocation; 

public class AroundLoggingAdvice implements MethodInterceptor{ 

public Object invoke(MethodInvocation arg0) throws Throwable { 

System.out.println("메소드실행전..."); 
Object returnedObj = arg0.proceed(); 
System.out.println("메소드실행후..."); 
return returnedObj; 




-------------------- BeforeLoggingAdvice.java --------------------- 

package onj.aop.programming2; 

import java.lang.reflect.Method; 

import org.springframework.aop.MethodBeforeAdvice; 

public class BeforeLoggingAdvice implements MethodBeforeAdvice { 


public void before(Method method, Object[] args, Object target) 
throws Throwable { 
System.out.println("사전 충고받으시오::" + method); 




-------------------- ThrowsLoggingAdvice.java --------------------- 

package onj.aop.programming2; 

import org.springframework.aop.ThrowsAdvice; 

public class ThrowsLoggingAdvice implements ThrowsAdvice{ 

public void afterThrowing(Throwable throwable) { 
System.out.println(">>>>>>>> 에러발생 " + throwable); 

}

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

평일주간(9:30~18:20) 개강
(5/12)[기업100%환급]Spring ,MyBatis,Hibernate실무과정
(5/12)안드로이드개발자과정
(5/19)C#4.0,ADO.NET,Network 프로그래밍
(5/19)Spring3.X, MyBatis, Hibernate실무과정
(5/19)[기업100%환급]PL/SQL,ORACLE HINT,TUNING
(5/21)[교육전취업확정]Spring,MyBatis,XPlatform실무프로젝트과정
(5/26)SQL초보에서실전전문가까지
(5/27)JAVA,ORACLE 실무개발자과정

평일야간(19:00~21:50) 개강
(5/15)Spring3.X, MyBatis, Hibernate실무과정
(5/16)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(5/16)C#,ASP.NET마스터
(5/16)SQL초보에서실전전문가까지
(5/16)웹퍼블리싱 마스터
(5/19)안드로이드개발자과정
(5/29)JAVA&WEB프레임워크실무과정

주말(10:00~17:50) 개강
(5/10)Spring3.X, MyBatis, Hibernate실무과정
(5/17)웹퍼블리싱 마스터
(5/17)C#,ASP.NET마스터
(5/17)JAVA&WEB프레임워크실무과정
(5/17)SQL초보에서실전전문가까지
(5/17)안드로이드개발자과정
(5/24)닷넷실무자를위한WPF개발자과정

주말저녁(18:30~22:20) 개강
(5/17)자바&웹,jQUERY,스프링프레임워크
(5/17)SQL기초에서 Schema Object까지

댓글 없음:

댓글 쓰기