http://www.javaworld.com/javaworld/jw-11-2000/jw-1110-proxy.html
- Interestingly, you can have a proxy class that implements multiple interfaces. However, there are a few restrictions on the interfaces you implement. It is important to keep those restrictions in mind when creating your dynamic proxy:
- The proxy interface must be an interface. In other words, it cannot be a class (or an abstract class) or a primitive.
- The array of interfaces passed to the proxy constructor must not contain duplicates of the same interface. Sun specifies that, and it makes sense that you wouldn't be trying to implement the same interface twice at the same time. For example, an array
{ IPerson.class, IPerson.class }would be illegal, but the code{ IPerson.class, IEmployee.class }would not. The code calling the constructor should check for that case and filter out duplicates. - All the interfaces must be visible to the
ClassLoaderspecified during the construction call. Again, that makes sense. TheClassLoadermust be able to load the interfaces for the proxy. - All the nonpublic interfaces must be from the same package. You cannot have a private interface from package
com.xyzand the proxy class in packagecom.abc. If you think about it, it is the same way when programming a regular Java class. You couldn't implement a nonpublic interface from another package with a regular class either. - The proxy interfaces cannot have a conflict of methods. You can't have two methods that take the same parameters but return different types. For example, the methods
public void foo()andpublic String foo()cannot be defined in the same class because they have the same signature, but return different types (see The Java Language Specification). Again, that is the same for a regular class. - The resulting proxy class cannot exceed the limits of the VM, such as the limitation on the number of interfaces that can be implemented.
本文探讨了Java中动态代理接口实现时的限制条件,包括接口必须为接口类型、不能重复实现相同接口、所有接口必须可见且来自同一包、接口方法不能冲突以及代理类接口数量不能超过VM限制。
1238

被折叠的 条评论
为什么被折叠?



