4.10 Summary
Proxy is also the only way to dynamically create classes from inside the Java programming language.
All of the implementation classes are created at runtime, leaving only the specification to be maintained by the developers. This specification also allows the creation of proxy classes for interfaces that were not available when the application was compiled. This means that Proxy can work with dynamic loading to enhance application flexibility.
4.9 Pitfalls of using Proxy
When this invoke method forwards the call p.equals(p) where p contains a reference to a proxy instance with a target of type PointImpl1, the return has the value false. This happens because the argument is a proxy instance, which is not of type PointImpl1.
public interface Point {
float getX();
float getY();
}
public class PointImpl1 implements Point {
private float x, y;
public PointImpl1( float x, float y ) { this.x = x; this.y = y; }
public float getX() { return x; }
public float getY() { return y; }
public boolean equals( Object obj ) {
if ( obj instanceof PointImpl1) {
PointImpl1 p = (PointImpl1)obj;
return p.x == x && p.y == y;
}
else
return false;
}
}
Now, consider another Point implementation shown in listing 4.19. Using this
implementation, p.equals(p) returns the expected value, true. The difference
between the two implementations is that while the first accesses values through
the concrete implementation, the second accesses these same values but through
the interface. The second succeeds because the proxy instance understands how
to respond to the interface.
public class PointImpl2 implements Point {
private float x, y;
public PointImpl2( float x, float y ) { this.x = x; this.y = y; }
public float getX() { return x; }
public float getY() { return y; }
public boolean equals( Object obj ) {
if ( obj instanceof Point) {
Point p = (Point)obj;
return p.getX() == x && p.getY() == y;
}
else
return false;
}
}
The general rule is if a class is expected to be proxied, a method parameter that
has that class as its type should be accessed through the interface. This can be
problematic if the interface does not provide the necessary access (as would be
the case if Point interface did not have both accessors).
Listing 4.19 PointImpl2 class
类一旦被代理,执行时的Object实例就已经不再是被代理的类,而是代理类
分享到:
相关推荐
package cn.sxt.dynamicproxy; import java.util.ArrayList; import java.util.List; import cn.sxt.service.UserService; import cn.sxt.service.UserServiceImpl; public class Client { public ...
SpringIOC,DI+dynamic proxy 实现盗版AOP的源代码
我自己用eclipse写的java代码,可以直接用eclipse导入,也可以直接用java -jar proxy_sample.jar执行 代码量很小,尽量通过注释进行说明 本例实现了InvocationHandler接口,代码具有典型性 在研究代理模式(Proxy...
NULL 博文链接:https://cn-done.iteye.com/blog/1743191
Castle框架注入依赖所需dll Version:2.1.0.0
Castle Dynamic Proxy代理使用
DynamicProxy.zip
DynamicProxy,源码DynamicProxy,源码DynamicProxy,源码
java 反射与动态代理Reflection&DynamicProxy,源码展示
这是动态代理的简单实例,方便学,入手.这个例子非常的简单哦!
绿色软件,无需安装,可以自动添加、修改代理服务器ip地址
配合 LinFu.DynamicProxy 1.0.4.18998 NHibernate使用中框架动态代理方式 LinFu框架 如果使用LinFu.DynamicProxy动态代理,引用NHibernate.ByteCode.LinFu.dll程序集并配置proxyfactory.factory_class节点为 ...
JAVA 通过proxy代理方式访问internet资源,
NULL 博文链接:https://lionkingzw-hotmail-com.iteye.com/blog/485923
JdkProxy.java
代理模式java代码 Proxy(4) 开发宝典......
java static proxy dynamic proxy
java proxy 代理,全套开发code,带注释,以及相关工具
是一个实现了代理服务的类。当有客户端连接时调用serve()方法,可以在客户和服务器端传输数据。