[英]How can I replace the instanced of a type by wrapping around the constructor?
I have a legacy lib that creates instances of BeanProxy
. 我有一个遗留的lib,它创建了BeanProxy
实例。 Unfortunately this implementation has a flaw I would like to fix. 不幸的是,这个实现有一个我想修复的缺陷。 As I don't want to start working with patched libs, I wanted to create an Aspect wrapping the construction of BeanProxy
and return an instance of my modified BeanProxy
sub-type. 由于我不想开始使用修补的库,我想创建一个包装BeanProxy
构造的BeanProxy
并返回我修改过的BeanProxy
子类型的实例。
I created the following Aspect and it is correctly woven and called whenever a new instance of BeanProxy
is created: 我创建了以下Aspect,只要创建了一个新的BeanProxy
实例,它就会被正确编织和调用:
@Aspect
public class CWebBeanProxyInjectingAspect {
@Pointcut("execution(public flex.messaging.io.BeanProxy.new(..))")
void createBeanProxy() {}
@Around("createBeanProxy()")
public Object createAlternateBeanProxy(final ProceedingJoinPoint pjp) throws Throwable {
System.out.println("createAlternateBeanProxy");
final Object result = pjp.proceed();
System.out.println(result);
return result;
}
}
Unfortunately result
is always null
... what am I doing wrong? 不幸的是result
总是null
......我做错了什么? What do I have to change? 我需要改变什么? I should mention, that I am using AspectJ LoadTimeWeaving and the spring-instrument-3.1.1.RELEASE.jar as agent. 我应该提一下,我使用AspectJ LoadTimeWeaving和spring-instrument-3.1.1.RELEASE.jar作为代理。
Constructor execution
don't return anything (is void
). 构造函数execution
不返回任何内容( void
)。 If you want to return the created object, use call
in your pointcut: 如果要返回创建的对象, call
在切入点中使用call
:
@Pointcut("call(public flex.messaging.io.BeanProxy.new(..))")
void createBeanProxy() {}
see Contstructor call and Constructor execution in http://www.eclipse.org/aspectj/doc/next/progguide/semantics-joinPoints.html 请参阅http://www.eclipse.org/aspectj/doc/next/progguide/semantics-joinPoints.html中的 Contstructor调用和构造函数执行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.