Announcement Announcement Module
Collapse
No announcement yet.
Invoking a different method on proxy target Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Invoking a different method on proxy target

    I am trying to implement an interceptor where I sometimes need to call a different method on the proxy target. I currently implement only the AOP Alliance MethodInterceptor interface which obviously won't give me access to the proxy. I am also unsure on how to setup ReflectiveMethodInvocation or extend it. Do I have to write my own proxy class? What is the best way of doing this?

    Example:
    private Object invokeGetById(MethodInvocation methodInvocation, Long arg) {
    Class declaringClazz = methodInvocation.getMethod().getDeclaringClass();
    Method method = declaringClazz.getMethod("getById", new Class[] {arg.getClass()});
    invokeRet = method.invoke(declaringClazz, new Object[] {arg});
    }

    The problem is declaringClazz is a class and not an instance. Really its the target of the proxy so if I had access to that everything would be great.

    Thanks in advance.
    -Xavier

  • #2
    In Spring you can cast the MethodInvocation to ReflectiveMethodInvocation and call getProxy(). getThis() will give you the target.

    In Spring 2.0 we recommend the AspectJ style. You can bind the proxy (this) or target (target) using a pointcut expression, or you can access it reflectively using the JoinPoint interface. Here's an example:

    Code:
    @Before(value="execution(void set*(*)) && this(mixin)", argNames="mixin")
    public void checkNotLocked(
    		Lockable mixin)  // Bind to arg
    {
    	// Can also obtain the mixin (this) this way
    	//Lockable mixin = (Lockable) jp.getThis();
    	if (mixin.locked()) {
    		throw new IllegalStateException();
    	}
    }

    Comment


    • #3
      You would need to use Around advice in AspectJ style to be able to call a different method...

      Comment


      • #4
        Thanks for the reply. I re-evaluated and ended up spliting my handling into a Spring AOP MethodBeforeAdvice and MethodAfterAdvice, which gave me access to the target.

        Thanks again,
        Xavier

        Comment

        Working...
        X