如果你有一辆实现IVehicle的类车,并且你想将它包装在一个装饰器中,它将所有的呼叫转发给汽车并计算它们,你会怎么做?
在Ruby中,我可以在没有任何方法的情况下构建装饰器,并使用method_missing将所有调用转发给car对象.
在Java中,我可以构建一个Proxy对象,该对象通过一种方法运行所有代码,然后再转发它.
在C#中我能做些类似的事吗?
更新:
基于answeres和我读过的关于System.Reflection.Emit的内容,应该可以编写一个类似于此的方法:
Type proxyBuilder(Type someType, delagate functionToBeApplied, Object forward)
Run Code Online (Sandbox Code Playgroud)
其中type实现someType的所有接口,执行functionToBeApplied,然后在返回其返回时将方法调用转发给object.
是否有一些lib可以做到这一点,还是我必须自己编写?
所以我正在玩geotools,我想我会代理他们的一个数据访问类,并跟踪它们在代码中的使用方式.
我编写了一个动态代理并在其中包含了一个FeatureSource(接口),并且很高兴.然后我想看看一些由featureSource返回,以及在传递对象,因为FeatureSource做的主要工作是返回的FeatureCollection(FeatureSource类似于一个SQL数据源,并以的FeatureCollection SQL语句).
在我的InvocationHandler我刚刚通过的调用底层对象,打印出目标类/方法/指定参数和结果,我去了,但是对于返回的FeatureCollection(另一个接口)调用,我裹着该对象在我的代理服务器(在相同的类,但一个新的实例,应该不重要吗?)并返回它.BAM!Classcast异常:
java.lang.ClassCastException: $Proxy5 cannot be cast to org.geotools.feature.FeatureCollection
at $Proxy4.getFeatures(Unknown Source)
at MyClass.myTestMethod(MyClass.java:295)
Run Code Online (Sandbox Code Playgroud)
调用代码:
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = ... // create the FS
featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) FeatureSourceProxy.newInstance(featureSource, features);
featureSource.getBounds();// ok
featureSource.getSupportedHints();// ok
DefaultQuery query1 = new DefaultQuery(DefaultQuery.ALL);
FeatureCollection<SimpleFeatureType, SimpleFeature> results = featureSource.getFeatures(query1); //<- explosion here
Run Code Online (Sandbox Code Playgroud)
代理人:
public class FeatureSourceProxy implements java.lang.reflect.InvocationHandler {
private Object target;
private List<SimpleFeature> features;
public static Object newInstance(Object obj, List<SimpleFeature> features) {
return java.lang.reflect.Proxy.newProxyInstance(
obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(),
new FeatureSourceProxy(obj, features)
);
}
private …Run Code Online (Sandbox Code Playgroud) 我有一个Spring应用程序(Spring Batch不是Web应用程序).在测试类中,我想获取对给定类型的所有bean的访问权限.
我知道在Spring中你通常应该使用IOC并让容器注入你的bean.但是在这种情况下,我想循环遍历给定类(org.springframework.batch.item.database.JdbcCursorItemReader)的可变数量的bean,并做一些事情(希望它是一个只连接的单元/集成测试它到数据库并读取1行,因此我们可以在测试时确认系统中的所有JdbcCursorItemReader都有有效的SQL和行映射器).
问题1)我一次只能获得一个豆子.我可以让我的类实现BeanFactoryAware来获取对beanfactory的引用.然后我可以做beanFactory.getBean("name"); 访问单个bean.我该如何获得所有豆类?我可以遍历并删除不属于我想要的类的那些..但不知何故,我需要beanfactory知道的所有bean的列表或者其他东西.
问题2)我从beanfactory回来的bean是一个代理.如果我尝试强制转换并使用我的bean,我会得到类似 java.lang.ClassCastException:$ Proxy0无法转换为org.springframework.batch.item.database.JdbcCursorItemReader
在Windows上,创建"代理DLL"或多或少是常见的,代替原始DLL并将调用转发给它(在需要时执行任何其他操作之后).你可以在这里和这里阅读它.
然而,Linux下的shlib munging文化是完全不同的.首先,LD_PRELOAD是Linux下的ld.so的内置功能,它只是将单独的shlib注入进程并使用它定义为覆盖的任何符号.而这种"注入"技术似乎定义了整个思维方向 - 这里是一个典型的ELF黑客工具或者这个问题,绅士似乎与我有相同的用法,但首先要问他如何修补现有的二进制文件.
不用了,谢谢.我不想注入或修改那些也不是我的东西.我想要做的就是创建一个独立的代理shlib,它将调用原始代理.理想情况下,会有一个工具可以用原始的.so提供,并创建一个C源代码,它只是重定向到原始的函数,同时让我轻松覆盖我想要的任何东西.那么,这样的工具在哪里?;-) 谢谢.
我已经使用了带有Delphi XE2的WSDL导入程序,它生成了一个如下所示的例程,不包括我试图使用代理服务器的3条注释行.
function GetIXYZService(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): IXYZService;
const
defWSDL = 'https://server/XYZService.svc?wsdl';
defURL = 'https://server/XYZService.svc';
defSvc = 'Company.XYZ.Services.XYZService';
defPrt = 'BasicHttpBinding_IXYZService';
var
RIO: THTTPRIO;
begin
Result := nil;
if (Addr = '') then
begin
if UseWSDL then
Addr := defWSDL
else
Addr := defURL;
end;
if HTTPRIO = nil then
RIO := THTTPRIO.Create(nil)
else
RIO := HTTPRIO;
try
// RIO.HTTPWebNode.Proxy := 'server_ip:port';
// RIO.HTTPWebNode.Username := 'username';
// RIO.HTTPWebNode.Password := 'password';
Result := (RIO as ISSOService);
if UseWSDL then …Run Code Online (Sandbox Code Playgroud) 我正在开发一个代理,并通过查看W3指南来调查标题.我的代理将传入的REMOTE-ADDR添加到X-FORWARDED-FOR标头,但我不确定这与Via标头的比较.我查看了维基百科页面和此列表
Via: 1.0 fred, 1.1 example.com (Apache/1.1)
Run Code Online (Sandbox Code Playgroud)
但是我没有找到关于如何建立它的任何指示.
我有混合堆栈:EJB和Spring.对于自动装配Spring到EJB我使用SpringBeanAutowiringInterceptor(不确定这是否会影响我的问题).
尝试以下列方式自动装配bean:
@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS, value="singleton")
@Repository
public class ClassA imlpements IfA {
...
}
@Scope(value="singleton")
@Repository
public class ClassB {
@Autowired
private ClassA classA;
...
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Could not autowire field: private ClassA ClassB.classA; nested exception is java.lang.IllegalArgumentException: Can not set ClassA field ClassB.classA to com.sun.proxy.$Proxy257. Please see server.log for more details.
Run Code Online (Sandbox Code Playgroud)
据我所知,问题是使用了JDK代理(作为ClassA实现接口IfA),而我需要cglib 代理.
但我不明白.基于文档,注释:@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS,应该在这里完成工作.
好吧,我不能选择使用接口(作为字段类型).
知道如何强制cglib代理在这里吗?
编辑:
不确定它是否相关,但我正在运行glassfish 3.1.x.
和stacktrace(部分,因为我不能在这里公开所有类名:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'classB': Injection of …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过npm安装bower来设置角度种子项目,但是遇到了由我的公司代理引起的错误.
retry Request to https://bower.herokuapp.com/packages/angular failed with ECONNRESET, retrying in 1.2s
bower retry Request to https://bower.herokuapp.com/packages/angular-route failed with ECONNRESET, retrying in 1.4s
bower retry Request to https://bower.herokuapp.com/packages/angular-loader failed with ECONNRESET, retrying in 1.9s
bower retry Request to https://bower.herokuapp.com/packages/angular-mocks failed with ECONNRESET, retrying in 1.3s
bower retry Request to https://bower.herokuapp.com/packages/html5-boilerplate failed with ECONNRESET, retrying in 1.8s
bower retry Request to https://bower.herokuapp.com/packages/angular failed with ECONNRESET, retrying in 2.7s
Run Code Online (Sandbox Code Playgroud)
我试图遵循我在另一个堆栈溢出帖子中看到的建议来编辑我的.bowerrc文件以添加有关代理的详细信息.但是,一旦我这样做,我得到这个错误:
/Users/t821714/Projects/customer/customer/node_modules/bower/node_modules/bower-config/lib/util/rc.js:56
throw error;
^
Error: Unable to parse /Users/t821714/Projects/customer/customer/.bowerrc: Unexpected token …Run Code Online (Sandbox Code Playgroud) 我正在创建一个chrome扩展程序,用于动态更改代理设置.但在"chrome.proxy.settings.set"函数中,无法为代理配置userName和密码.因此,每次更改设置后我都必须手动输入凭据.
但我发现在Proxy Auto Auth扩展中有一些如何以编程方式配置用户名和密码.我找不到任何关于如何做到这一点的文档.
如果有人可以提供帮助,我将不胜感激.
我是公司代理的后面,我需要将设置设置为npm.唯一的问题是我的用户名包含一个\字符.代理设置应如下所示:http://PREFIX\username:password@proxy.company.com:port但最终结果如下:http://prefix/username:password@proxy.company.com:port这是完全错误的.
投入%5C而\不是不起作用.既没有输入用户名和密码,也没有输入整个网址"".
我知道这个问题已被多次询问,但没有一个解决方案为我做过.我在一台Windows机器上.如果您有任何建议,我将不胜感激.