I would like to override a method in an object that\'s handed to me by a factory that I have little control over.
My specific problem is that I want to override the
Another proxying-related solution: you could use Aspects to override a method on a given object without subclassing it yourself. This is especially appropriate and common for logging. This example uses spring-aop.
class Example {
final Foo foo;
Example(Foo original) {
AspectJProxyFactory factory = new AspectJProxyFactory();
factory.setTarget(original);
factory.addAspect(FooAspect.class);
foo = (Foo) factory.getProxy();
}
@Aspect
static class FooAspect {
@Before("execution(Foo.doBar())")
Object beforeDoBar() {
// My own activity
}
}