Java的动态代理(dynamic proxy)(二)

2014-11-24 08:14:42 · 作者: · 浏览: 5
* --after running...
* */
复制代码
可以看出,对IVehical接口的调用,会交由Proxy的invoke方法处理,并在不改变run()的源代码下,新增了动态的逻辑(before running/after running),这正式AOP所做的。
深入讲,Proxy.newInstance(ClassLoader loader, Class< >[] interfaces, InvocationHandler h)做了以下几件事.
(1)根据参数loader和interfaces调用方法 getProxyClass(loader, interfaces)创建代理类$Proxy.
$Proxy0类实现了interfaces的接口,并继承了Proxy类.
(2)实例化$Proxy0并在构造方法中把BusinessHandler传过去,接着$Proxy0调用父类Proxy的构造器,为h赋值,如下:
class Proxy{
InvocationHandler h=null;
protected Proxy(InvocationHandler h) {
this.h = h;
}
...
}
另外,如果将invoke()方法代码改成如下:
复制代码
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("--before running...");
// Object ret = method.invoke(vehical, args);
((IVehical)proxy).run();
System.out.println("--after running...");
return null;
}
复制代码
结果会是因为run()方法会引发invoke(),而invoke()又执行run(),如此下去变成死循环,最后栈溢出
所以invoke 接口中的proxy参数不能用于调用所实现接口的某些方法(见参考4)。
2. 利用动态代理实现设计模式,修饰器和适配器:
见参考5
3. 在项目中,可以使用动态代理,获取配置文件,非常方便且有优势:
复制代码
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Value {
/**
* The actual value expression: e.g. "#{systemProperties.myProp}".
*/
String value();
}
/**
* config interfaces, map the config properties file:
* db.url =
* db.validation = true
* db.pool.size = 100
*/
public interface IConfig {
@Value("db.url")
String dbUrl();
@Value("db.validation")
boolean isValidated();
@Value("db.pool.size")
int poolSize();
}
//proxy class
public final class ConfigFactory {
private ConfigFactory() {}
public static IConfig create(final InputStream is) throws IOException{
final Properties properties = new Properties();
properties.load(is);
return (IConfig) Proxy.newProxyInstance(IConfig.class.getClassLoader(),
new Class[] { IConfig.class }, new PropertyMapper(properties));
}
public static final class PropertyMapper implements InvocationHandler {
private final Properties properties;
public PropertyMapper(Properties properties) {
this.properties = properties;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
final Value value = method.getAnnotation(Value.class);
if (value == null) return null;
String property = properties.getProperty(value.value());
if (property == null) return (null);
final Class< > returns = method.getReturnType();
if (returns.isPrimitive())
{
if (returns.equals(int.class)) return (Integer.valueOf(property));
else if (returns.equals(long.class)) return (Long.valueOf(property));
else if (returns.equals(double.class)) return (Double.valueOf(property));
else if (returns.equals(float.class)) return (Float.valueOf(property));
else if (returns.equals(boolean.class)) return (Boolean.valueOf(property))