自分で書いた動的エージェントの小さな例


package com.chinasoft.service;

public interface UserService {
	
	public void add();

}

 
package com.chinasoft.service;

public class UserServiceimpl implements UserService {

	
	public void add() {
		System.out.println(" ");
	}

}

 
public class ServiceProxy implements InvocationHandler{
	private Object obj;
	
	public ServiceProxy(Object obj) {
			super();
			this.obj = obj;
		}
// 
public static Object getProxy(Object object){
	  Object ret= Proxy.newProxyInstance(object.getClass().getClassLoader(), 
			                  object.getClass().getInterfaces(), 
			                  new ServiceProxy(object));
	   return ret;
   }
	// InvocationHandler , 
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
	    System.out.println(" ..........");          // 
	    System.out.println(" !!!!!!!!");
		
	    Object ob=method.invoke(obj, args);   //obj 
		
	    System.out.println(" !!!!!!!!");    // 
		return ob;
	}
	
}

 
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		UserServiceimpl  ul=new UserServiceimpl();    //new 
		UserService us=(UserService) ServiceProxy.getProxy(ul);// 
		us.add();

	}

}

出力結果:
 ..........
 !!!!!!!!
 
 !!!!!!!!