Java Proxy动态代理
先看个例子:
接口
package example;
public interface Basic {
public void hello();
}
接口的实现类
package example;
public class BasicService implements Basic {
public void hello() {
Sysytem.out.println("Hello, world");
}
}
实现了InvocationHandler接口的类
package example;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LogHandler implements InvocationHandler {
private Logger logger = Logger.getLogger(this.getClass().getName());
private Object delegate;
public Object bind(Object delegate) {
this.delegate = delegate;
return Proxy.newProxyInstance(delegate.getClass().getClassLoader(), delegate.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = null;
try {
log("method starts..." + method);
result = method.invoke(delegate, args);
log("method ends..." + method);
} catch (Exception e) {
log(e.toString());
}
&nb
相关文档:
Java 静态代理和动态代理
http://kuangbaoxu.javaeye.com/blog/192804
关键字: 静态代理与动态代理
1. 代理模式主要有两种:静态代理和动态代理
2. 静态代理:
比如要在输出“HelloWorld”前打印一个字符串“Welcome”
A:先定义一个接口类
Java代码
pac ......
/**
* 获得当前时间一小时前的时间,格式化成yyyy-MM-dd HH:mm:ss:SS<br>
*
* @return 当前时间一小时前的时间
*/
public String getOneHours ......
//静态块(static block):如下所示
public class StaticBlock1
{
static
{
System.out.println("static block");
......
Database
MySQL
PostgreSQL
Firebird(suitable for embedded database)
HSQLDB(160K)
DB Tie
Hibernate
Ibatis
JDO
OSCache
JBossCache
Business Tie
Spring
UI Tie
Structs
JSF
Tapestry
Webwork
Flex
DWR(Ajax
Framework)
ZK(Ajax
Framework)
Dojo(Ajax
Framework)
Development Tools
......