2014년 5월 1일 목요일

JAVA JDK java.lang.Proxy를 이용한 AOP 예제, 오라클자바커뮤니티 스프링강좌, spring framework4 교육

JAVA JDK java.lang.Proxy를 이용한 AOP 예제, 오라클자바커뮤니티 스프링강좌, spring framework4 교육 

1.     HelloWorld  Interface를 작성한다.
Proxy는 인터페이스를 통해서 객체를 생성하는 방식이므로 반드시 인터페이스가 필요하다.
       
[HelloWorld.java]
        package proxy;
 
/**
 * Proxy 인터페이스를 통해서 객체를 생성하는 방식이므로
 * 반드시 인터페이스가 필요하다.
 * Spring에서 인터페이스가 없는 경우에는 CGLIB 이용한다.
 * @author tatata
 *
 */
public interface HelloWorld {
    public String sayHello(String msg);
}
 
2.     인터페이스 구현 클래스 작성
[HelloWorldImpl.java]
package proxy;
 
public class HelloWorldImpl implements HelloWorld{
    public String sayHello(String msg) {
        return "hi~ " + msg;       
    }
}
 
3.     전통적인 클라이언트 프로그램 작성
[OldClient.java]
package proxy;
 
/**
 * 전통적인 형태의 클라이언트 모듈임.
 * 인터페이스를 통한 느슨한 결합외 별다른 없음...
 * @author tatata
 *
 */
 
public class OldClient {
 
       /**
        * @param args
        */
       public static void main(String[] args) {
             // TODO Auto-generated method stub
        HelloWorld hello = new HelloWorldImpl();
        System.out.println(hello.sayHello("JCLEE"));
       }
}
 
4.     OldClient 실행하여 결과를 확인 하자.
이클립스에서 클래스선택 후 à 마우스 우측버튼 à Run as à Java Applicatopn
 
5.     Proxy를 위한 Handler 클래스 작성
 
[HelloHandler.java]
package proxy;
 
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
 
public class HelloHandler implements InvocationHandler {
       private Object targetObj = null;
      
       public HelloHandler(Object target) {
             this.targetObj = target;
       }
      
       public Object invoke(Object obj, Method method, Object[] args) throws Throwable {
        try {
        System.out.println(" <<<<<<<<<< Start...");
        System.out.println(method.getName());
        System.out.println(args);
       
        return method.invoke(targetObj, args);
        }
        catch(Exception e) {
        throw e;
        }
        finally {
        System.out.println(" <<<<<<<<<< END...");
        }
       }
 
}
 
6.     새로운 클라이언트 프로그램 작성
 
[NewClient.java]
package proxy;
 
import java.lang.reflect.*;
 
/**
 * 객체의 메쏘드를 호출할 직접 객체를 호출하는 방식이 아니라,
 * HelloHandler라는 프록시 객체를 이용하는 방식이다.
 * HelloHandler invoke라는 메쏘드를 통해서 지정된 객체의 함수를 실행하는데
 * invoke 함수에 내용을 추가 하면 다음과 같은 구성도 가능하다.
 * @author tatata
 *
 */
public class NewClient {
 
       /**
        * @param args
        */
       public static void main(String[] args) {
             // TODO Auto-generated method stub
       try {
           Class[] arrClass = {HelloWorld.class};
           
           //targetObj HelloWorldImpl 된다.
           HelloHandler handler = new HelloHandler(new HelloWorldImpl());
           
           HelloWorld helloProxy = (HelloWorld) Proxy.newProxyInstance(                                                 HelloWorld.class.getClassLoader(),
                                                    arrClass,
                                                    handler);
           
           System.out.println(helloProxy.sayHello("JCLEE"));
       }
       catch(Exception e) {
           e.printStackTrace();
       }
       }
 
}

오라클자바커뮤니티 오프라인 교육센터, 개발자 전문교육, 개인80%환급 오엔제이프로그래밍실무교육센터(www.onjprogramming.co.kr)

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

평일야간(19:00~21:50) 개강
(5/14)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(5/15)Spring3.X, MyBatis, Hibernate실무과정
(5/16)자바웹(JSP,Spring,MyBatis,XPlatform)프로젝트과정
(5/16)C#,ASP.NET마스터
(5/16)SQL초보에서실전전문가까지
(5/16)웹퍼블리싱 마스터
(5/19)안드로이드개발자과정
(5/20)개발자를위한PLSQL,SQL튜닝,힌트
(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까지



댓글 없음:

댓글 쓰기