创建动态代理服务
来源:互联网 发布:mac画流程图软件 编辑:程序博客网 时间:2024/04/30 16:44
Java 提供了强大的动态能力,在客户端可以只知道接口来调用本地或者远程的服务。
在客户端只关心接口是什么:
服务工厂返回一个服务:
DynamicBusinessServiceHandler是代理的关键,他可以拦截服务调用的方法,并且完成需要做的任务。
当然自定义的接口和实现不可少
在客户端只关心接口是什么:
/**
*DynamicProxyClient.java
*2007-4-13
*/
package com.kompakar.tut.proxy.dynamic;
/**
* <p>Project: proxyExample</p>
* <p>Title: DynamicProxyClient</p>
* <p>Description: </p>
* <p>Copyright: Copyright 2007.All rights reserved.</p>
* @author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
* @version 1.0
* @see
*/
public class DynamicProxyClient {
/**
* @param args
*/
public static void main(String[] args) {
MyBusiness server = BusinessServiceFactory.getDynamicBusinessProxy(MyBusiness.class);
System.out.println(server.echo("Alan!"));
}
}
*DynamicProxyClient.java
*2007-4-13
*/
package com.kompakar.tut.proxy.dynamic;
/**
* <p>Project: proxyExample</p>
* <p>Title: DynamicProxyClient</p>
* <p>Description: </p>
* <p>Copyright: Copyright 2007.All rights reserved.</p>
* @author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
* @version 1.0
* @see
*/
public class DynamicProxyClient {
/**
* @param args
*/
public static void main(String[] args) {
MyBusiness server = BusinessServiceFactory.getDynamicBusinessProxy(MyBusiness.class);
System.out.println(server.echo("Alan!"));
}
}
服务工厂返回一个服务:
/**
*BusinessFactory.java
*2007-4-13
*/
package com.kompakar.tut.proxy.dynamic;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
/**
* <p>Project: proxyExample</p>
* <p>Title: BusinessFactory</p>
* <p>Description: </p>
* <p>Copyright: Copyright 2007.All rights reserved.</p>
* @author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
* @version 1.0
* @see
*/
public class BusinessServiceFactory {
/**
* Gets proxy to depending upon debug status in Log4J.
*
* @return The proxy to the implementation.
*/
public static final <T> T getDynamicBusinessProxy(Class<T> serviceClass) {
InvocationHandler handler = new DynamicBusinessServiceHandler(serviceClass);
Class[] interfaces = new Class[] { serviceClass };
ClassLoader loader = BusinessServiceFactory.class.getClassLoader();
return (T) Proxy.newProxyInstance(loader, interfaces, handler);
}
}
*BusinessFactory.java
*2007-4-13
*/
package com.kompakar.tut.proxy.dynamic;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
/**
* <p>Project: proxyExample</p>
* <p>Title: BusinessFactory</p>
* <p>Description: </p>
* <p>Copyright: Copyright 2007.All rights reserved.</p>
* @author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
* @version 1.0
* @see
*/
public class BusinessServiceFactory {
/**
* Gets proxy to depending upon debug status in Log4J.
*
* @return The proxy to the implementation.
*/
public static final <T> T getDynamicBusinessProxy(Class<T> serviceClass) {
InvocationHandler handler = new DynamicBusinessServiceHandler(serviceClass);
Class[] interfaces = new Class[] { serviceClass };
ClassLoader loader = BusinessServiceFactory.class.getClassLoader();
return (T) Proxy.newProxyInstance(loader, interfaces, handler);
}
}
DynamicBusinessServiceHandler是代理的关键,他可以拦截服务调用的方法,并且完成需要做的任务。
/**
*DynamicBusinessHandler.java
*2007-4-13
*/
package com.kompakar.tut.proxy.dynamic;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* <p>Project: proxyExample</p>
* <p>Title: DynamicBusinessHandler</p>
* <p>Description: </p>
* <p>Copyright: Copyright 2007.All rights reserved.</p>
* @author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
* @version 1.0
* @see
*/
public class DynamicBusinessServiceHandler implements InvocationHandler {
private Class className;
public DynamicBusinessServiceHandler(Class name) {
this.className = name;
}
/**
* You can invoke any service as you wish,for exmaple: local object or a remote service by interface name;
*/
public Object invoke(Object proxy, Method meth, Object[] args) throws Throwable {
try {
Object impl = null;
impl = getServiceByInterfaceName(className);
Object result = meth.invoke(impl, args);
return result;
} catch (final InvocationTargetException ex) {
throw ex.getTargetException();
}
}
/**
* @param interfaceName
* @return
* @throws ClassNotFoundException
* @throws InstantiationException
* @throws IllegalAccessException
*/
private Object getServiceByInterfaceName(Class interfaceName) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
String interfaceString = interfaceName.getName();
String className = interfaceString + "Impl";
ClassLoader loader = DynamicBusinessServiceHandler.class.getClassLoader();
Class serviceClass = Class.forName(className, false, loader);
return serviceClass.newInstance();
}
}
*DynamicBusinessHandler.java
*2007-4-13
*/
package com.kompakar.tut.proxy.dynamic;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* <p>Project: proxyExample</p>
* <p>Title: DynamicBusinessHandler</p>
* <p>Description: </p>
* <p>Copyright: Copyright 2007.All rights reserved.</p>
* @author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
* @version 1.0
* @see
*/
public class DynamicBusinessServiceHandler implements InvocationHandler {
private Class className;
public DynamicBusinessServiceHandler(Class name) {
this.className = name;
}
/**
* You can invoke any service as you wish,for exmaple: local object or a remote service by interface name;
*/
public Object invoke(Object proxy, Method meth, Object[] args) throws Throwable {
try {
Object impl = null;
impl = getServiceByInterfaceName(className);
Object result = meth.invoke(impl, args);
return result;
} catch (final InvocationTargetException ex) {
throw ex.getTargetException();
}
}
/**
* @param interfaceName
* @return
* @throws ClassNotFoundException
* @throws InstantiationException
* @throws IllegalAccessException
*/
private Object getServiceByInterfaceName(Class interfaceName) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
String interfaceString = interfaceName.getName();
String className = interfaceString + "Impl";
ClassLoader loader = DynamicBusinessServiceHandler.class.getClassLoader();
Class serviceClass = Class.forName(className, false, loader);
return serviceClass.newInstance();
}
}
当然自定义的接口和实现不可少
/**
*IMyBusiness.java
*2007-4-13
*/
package com.kompakar.tut.proxy.dynamic;
/**
* <p>Project: proxyExample</p>
* <p>Title: IMyBusiness</p>
* <p>Description: </p>
* <p>Copyright: Copyright 2007.All rights reserved.</p>
* @author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
* @version 1.0
* @see
*/
public interface MyBusiness {
public abstract String echo(String name);
}
*IMyBusiness.java
*2007-4-13
*/
package com.kompakar.tut.proxy.dynamic;
/**
* <p>Project: proxyExample</p>
* <p>Title: IMyBusiness</p>
* <p>Description: </p>
* <p>Copyright: Copyright 2007.All rights reserved.</p>
* @author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
* @version 1.0
* @see
*/
public interface MyBusiness {
public abstract String echo(String name);
}
/**
*MyBusinessImple.java
*2007-4-13
*/
package com.kompakar.tut.proxy.dynamic;
/**
* <p>Project: proxyExample</p>
* <p>Title: MyBusinessImple</p>
* <p>Description: </p>
* <p>Copyright: Copyright 2007.All rights reserved.</p>
* @author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
* @version 1.0
* @see
*/
public class MyBusinessImpl implements MyBusiness{
public String echo(String name) {
return "hello, " + name ;
}
}
*MyBusinessImple.java
*2007-4-13
*/
package com.kompakar.tut.proxy.dynamic;
/**
* <p>Project: proxyExample</p>
* <p>Title: MyBusinessImple</p>
* <p>Description: </p>
* <p>Copyright: Copyright 2007.All rights reserved.</p>
* @author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
* @version 1.0
* @see
*/
public class MyBusinessImpl implements MyBusiness{
public String echo(String name) {
return "hello, " + name ;
}
}
- 创建动态代理服务
- 创建动态代理服务
- dubbo 创建服务代理
- 创建动态代理
- OSGI--动态创建服务
- java创建动态代理类
- 使用Cglib创建动态代理
- .Net动态代理Castle ( 了解创建动态代理 )
- 基于动态代理的远程服务
- WEB服务代理类的创建
- 代理一:代理、AOP面向方面编程、动态代理、创建动态代理、InvocationHandler运行原理
- 使用CGLIB包创建动态代理
- Spring系列(1)--创建动态代理类
- 动态创建客户端端代理代码
- 使用动态代理创建数据库连接池
- 使用cglib形式创建动态代理
- 动态代理类的创建实例
- Spring系列(1)--创建动态代理类
- 一个很漂亮的日历
- C#中如何建立Windows服務
- Dreamweaver构建Blog全程实录
- 基于GUI编程时,如何在客户端与服务器之间传递对象
- 删除的2次确认 ItemDataBound
- 创建动态代理服务
- MFC程序框架剖析
- Visual Basic COM基础讲座之(二)
- PHP操作MYSQL数据库类
- Oracle Sql优化笔记
- SQL Server一共有哪些数据类型
- 公司到底是怎么看我们的……
- protel99SE的快捷键
- 如何才能更好地用C#做3DGIS的二次开发呢?