使用Castle的动态代理时,我遇到了一些(我认为)奇怪的行为.
使用以下代码:
class Program
{
static void Main(string[] args)
{
var c = new InterceptedClass();
var i = new Interceptor();
var cp = new ProxyGenerator().CreateClassProxyWithTarget(c, i);
cp.Method1();
cp.Method2();
Console.ReadLine();
}
}
public class Interceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine(string.Format("Intercepted call to: " + invocation.Method.Name));
invocation.Proceed();
}
}
public class InterceptedClass
{
public virtual void Method1()
{
Console.WriteLine("Called Method 1");
Method2();
}
public virtual void Method2()
{
Console.WriteLine("Called Method 2");
}
}
Run Code Online (Sandbox Code Playgroud)
我期待获得输出:
JDK代理类仅接受工厂方法newProxyInstance()中的接口.
是否有可用的解决方法或替代实施?如果我必须将方法提取到接口以便使它们与代理一起使用,则用例是有限的.我想将它们包装起来,以便在运行时应用基于注释的操作.
public static <T> T getProxy(T obj) {
InvocationHandler ih = new InjectProxy( obj );
ClassLoader classLoader = InjectProxy.class.getClassLoader();
return (T) Proxy.newProxyInstance( classLoader, obj.getClass().getInterfaces(), ih );
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
Run Code Online (Sandbox Code Playgroud) 我可以定义一个派生自DynamicObject并支持接口(ICanDoManyThings)的类,而无需在接口中实现每个方法吗?
我正在尝试创建一个动态代理对象,并希望通过MyProxyClass.TryInvokeMember的实现来处理此类上的方法调用,这可能会也可能不会将它们传递给包装对象.
这可能吗?
谢谢
一个例子最好地解释了:
public interface IA {
void foo();
void bar();
}
public class A : IA {
public virtual void foo(){
Console.Write("foo");
bar(); //call virtual method
}
public virtual void bar(){
Console.Write("bar");
}
}
public class Interceptor : IInterceptor {
public void Intercept(IInvocation invocation)
{
Console.WriteLine("Intercepted: " + invocation.Method.Name);
invocation.Proceed();
}
}
Main(){
IA a = new A();
//proxy-ing an interface, given an implementation
IA proxy = new Castle.DynamicProxy.ProxyGenerator()
.CreateInterfaceProxyWithTarget(a, new Interceptor());
proxy.foo();
}
Run Code Online (Sandbox Code Playgroud)
我原本期望输出:
Intercepted foo
foo
Intercepted bar
bar …Run Code Online (Sandbox Code Playgroud) 什么是构建接口和/或抽象类的动态实现的事实上的解决方案?我基本上想要的是:
interface IMyEntity {
int getValue1();
void setValue1(int x);
}
...
class MyEntityDispatcher implements WhateverDispatcher {
public Object handleCall(String methodName, Object[] args) {
if(methodName.equals("getValue1")) {
return new Integer(123);
} else if(methodName.equals("setValue")) {
...
}
...
}
}
...
IMyEntity entity = Whatever.Implement<IMyEntity>(new MyEntityDispatcher());
entity.getValue1(); // returns 123
Run Code Online (Sandbox Code Playgroud) 如何让我的动态代理抛出检查异常?
我需要一个接口的透明包装器,有时会抛出检查过的异常,例如IOException.没有第三方AOP或编写我自己的代理是否可能?手动修改界面的所有20种方法也不是一种选择.
我正在尝试使用jetty7来构建透明的代理设置.想法是将原始服务器隐藏在jetty服务器后面,以便传入的请求可以以透明的方式转发到源服务器.
我想知道我是否可以使用jetty的ProxyServlet.Transparent实现来实现.如果是的话,任何人都可以给我一些例子.
解包动态代理以检索下面的原始对象的最佳方法是什么?动态代理已使用创建java.lang.reflect.Proxy.newProxyInstance()
谢谢.
将方法或函数作为数据传递的常用方法有哪些有用的定义,例如:
language-agnostic delegates closures function-pointers dynamic-proxy
我想在反序列化之后将单例范围的依赖项重新注入原型Spring bean.
假设我有一个Process bean,它依赖于Repository bean.Repository bean的作用域是单例,但Process bean是原型作用域.我会定期对序列进行序列化,然后对其进行反序列化.
class Process {
private Repository repository;
// getters, setters, etc.
}
Run Code Online (Sandbox Code Playgroud)
我不想序列化和反序列化存储库.我也不想将"瞬态"放在成员变量上,该成员变量在Process中包含对它的引用,也不是对某种代理的引用,或者除了声明为Repository的普通旧成员变量之外的任何东西.
我想我想要的是让Process依赖于一个可序列化的代理,该代理指向(通过瞬态引用)到Repository,并且在反序列化时,可以再次找到Repository.我怎么能自定义Spring呢?
我想我可以使用代理来保存依赖引用,就像.我希望我可以使用那种确切的技术.但是我看到Spring生成的代理不是可序列化的,并且文档说如果我将它与单例bean一起使用,我将得到一个例外.
我可以在单例bean上使用自定义作用域,当要求自定义作用域bean时,它总是提供代理.这是一个好主意吗?其他想法?
dynamic-proxy ×10
java ×5
c# ×3
proxy ×2
c#-4.0 ×1
closures ×1
delegates ×1
dynamic ×1
jetty ×1
reflection ×1
spring ×1
transparent ×1