- 浏览: 74212 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
import junit.framework.TestCase;
import junit.framework.Assert;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.lang.reflect.InvocationHandler;
public class DynamicProxyTest extends TestCase {
private Object proxy;
public void setUp() throws Exception
{
proxy = makeAProxyWhichExtendsTestInterface();
}
private Object makeAProxyWhichExtendsTestInterface()
{
return Proxy.newProxyInstance(TestInterface.class.getClassLoader(), new Class[]{TestInterface.class},new TestInvocationHandler());
}
private class TestInvocationHandler implements InvocationHandler
{
public Object invoke(Object proxy,Method method,Object[] args) throws Throwable
{
if ("operationA".equals(method.getName()))
{
System.out.println("operationA in proxy");
}
else if ("operationB".equals(method.getName()))
{
System.out.println("operationB in proxy");
}
else if ("equals".equals(method.getName()))
{
return true;
}
else if ("hashCode".equals(method.getName()))
{
return 100;
}
else if ("toString".equals(method.getName())){
return "proxy.toString()";
}
return null;
}
}
public void testProxyDoesExtendTheInterface
()
{
Assert.assertTrue(proxy instanceof TestInterface);
}
public void testProxyClassIsPublicAndFinalButNotAbstract
()
{
Assert.assertTrue(Modifier.isPublic(proxy.getClass().getModifiers()));
Assert.assertTrue(Modifier.isFinal(proxy.getClass().getModifiers()));
Assert.assertFalse(Modifier.isAbstract(proxy.getClass().getModifiers()));
}
public void testProxyInheritedFromJavaLangReflectProxy
()
{
Assert.assertSame(java.lang.reflect.Proxy.class,proxy.getClass().getSuperclass());
}
public void testUnqualifiedNameOfProxyStartsWith
$Proxy()
{
Assert.assertTrue(proxy.getClass().getSimpleName().startsWith("$Proxy"));
}
public void testToStringOfProxyIsHandledByInvocationHandler
()
{
Assert.assertEquals("proxy.toString()",proxy.toString());
}
public void testEqualsOfProxyIsHandledByInvocationHandler
()
{
Assert.assertEquals(proxy,makeAProxyWhichExtendsTestInterface());
Assert.assertEquals(proxy,null);
}
public void testhashCodeOfProxyIsHandledByInvocationHandler
()
{
Assert.assertEquals(100,proxy.hashCode());
Assert.assertEquals(100,makeAProxyWhichExtendsTestInterface().hashCode()); }
public interface TestInterface
{
void operationA();
void operationB();
}
}
发表评论
-
How to be a Programmer: A Short,Comprehensive,and Personal Summary
2013-10-28 10:38 619well written. http://samizdat ... -
js module pattern
2013-10-12 16:21 438http://www.adequatelygood.com/ ... -
GZip compressing HTML, JavaScript, CSS etc. makes the data sent to the browser s
2013-07-31 15:48 686this is fun. http://tutorials ... -
java collection matrix
2012-08-07 11:24 783http://www.janeve.me/articles/w ... -
ghost text (aka in-field text)
2012-04-01 11:18 740http://archive.plugins.jquery.c ... -
What is Optimistic Locking vs. Pessimistic Locking
2011-09-09 16:50 865What is Optimistic Locking vs. ... -
what is DAO
2011-04-15 13:42 809http://java.sun.com/blueprints/ ... -
indenting xml in vim with xmllint
2011-01-10 09:48 730I added to my “.vimrc” file: ... -
css sprite
2010-12-15 16:57 712http://css-tricks.com/css-sprit ... -
最牛B 的 Linux Shell 命令
2010-10-30 00:08 745http://hi.baidu.com/hy0kl/blog/ ... -
GPS Bearing VS Heading
2010-10-21 15:40 1695http://gps.about.com/od/glossar ... -
Document Type Declaration
2010-07-19 22:01 861Document Type Declaration h ... -
XML Declaration must be the first line in the document.
2010-06-12 17:54 931The XML declaration typically a ... -
UCM
2010-05-08 11:41 795Two links about UCM The power ... -
What is an MXBean?
2010-01-28 11:10 815refer to http://weblogs.java. ... -
why wait() always in a loop
2010-01-19 00:17 858As we know ,jdk API doc suggest ... -
Locks in Java
2010-01-18 22:48 958copied from http://tutorials.je ... -
use jps instead of ps to find jvm process
2010-01-11 14:21 842copied from http://java.sun.com ... -
My first error of Hello Wolrd Struts
2010-01-04 09:10 887It's my first time to touch Str ... -
Unit Testing Equals and HashCode of Java Beans
2009-12-29 10:07 1334copy from http://blog.cornetdes ...
相关推荐
using Castle.DynamicProxy; public class SimpleSamepleEntity { public virtual string Name { get; set; } public virtual int Age { get; set; } public override string ToString() { return string....
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 ...
本文将深入探讨如何使用Spring的IOC和DI特性,结合动态代理(Dynamic Proxy)来实现一种类似AOP(面向切面编程)的功能,以达到在不修改原有代码的情况下增强或拦截方法调用的目的。 **一、Spring IOC(控制反转)*...
《解决IBatisNET v1.x在.NET 4.0下'Ambiguous match found'问题:Castle.DynamicProxy深度解析》 在.NET开发过程中,框架的选择和兼容性问题是开发者常常面临的挑战。其中,IBatisNET作为一款优秀的数据访问层框架...
Castle框架注入依赖所需dll Version:2.1.0.0
在本示例中,`ChannelFactory` 和 `DynamicProxy` 是两个关键概念,它们与WCF中的动态调用密切相关。 **ChannelFactory** 是WCF客户端编程模型中的一个重要组件。它是一个工厂模式的实现,用于创建与服务契约相匹配...
这是动态代理的简单实例,方便学,入手.这个例子非常的简单哦!
DynamicProxy.zip
Java反射(Reflection)和动态代理(Dynamic Proxy)是Java编程中的高级特性,它们为程序提供了强大的功能和灵活性。本文将深入探讨这两个主题,并通过源码分析来加深理解。 首先,让我们了解一下Java反射。反射机制...
解决VS报错:未能加载文件或程序集“LinFu.DynamicProxy, Version=1.0.3.14911, Culture=neutral, PublicKeyToken=62a6874124340d6e”或它的某一个依赖项。系统找不到指定的文件。 把该文件放到bin文件夹就好了。
**Java动态代理(Dynamic Proxy)** Java动态代理是Java提供的一种机制,可以在运行时创建具有特定接口的代理类。这通常用于实现AOP(面向切面编程)或事件处理等。主要由java.lang.reflect.Proxy和java.lang....
动态代理(DynamicProxy)是Java中一种强大的设计模式,它允许在运行时创建代理对象,这些代理对象可以作为原对象的“代理”,在调用原对象的方法时添加额外的功能,如日志、性能监控、事务处理等。在Java中,`java....
例如,通过MEF加载实现`INotifyDataErrorInfo`接口的验证服务,这些服务可以利用Castle DynamicProxy来拦截和增强视图模型的方法调用,进行数据验证。同时,Caliburn.Micro则负责处理视图和视图模型的交互,提供简单...
动态代理主要由两个核心类组成:`java.lang.reflect.Proxy` 和 `java.lang.reflect.InvocationHandler`。`Proxy` 类是用于创建动态代理对象的工厂,而`InvocationHandler`接口则定义了代理对象如何处理方法调用的...
主要写了静态代理、动态代理、还写了URL的使用,其实就是下载的原理了。 动态代理动态代理动态代理动态代理
在Java中,动态代理主要依赖于两个接口:`java.lang.reflect.InvocationHandler` 和 `java.lang.reflect.Proxy`。 `InvocationHandler` 接口定义了一个方法 `invoke()`,该方法会在代理对象的方法被调用时执行。...
动态代理框架源码 /// 说明: /// 在第一次需要用到代理时动态生成代理类,之后的使用均调用已经生成的代理类 /// 规则: /// 1.不能为接口创建代理类代理类 ... /// 2.... /// 3.... /// 1.... /// 2....