Results 1 to 5 of 5

Thread: problem with MethodInterceptor

  1. #1
    Join Date
    Jul 2005
    Posts
    6

    Default problem with MethodInterceptor

    Hi all,
    I have one method in my interface which I want to be intercepted.But when I am calling my getBean() method to get the bean from the application context,the log file of my application is showing that the toString() method is intercepted although there is no toString() method in my interface.Why it is doing so.Is spring calling this toString() method of object class internally.Please express your views.

    with regards,
    ajse

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Can you post the logs and your configuration? You might be doing class proxying which will intercept all calls not just toString().
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3

    Default

    There is no toString() method in your interface, but there is one in the Object class which is the superclass of all classes.

    Now what seems to be happening in yor case is that the interceptor is configured on the instance(target) itself class instead of the interface?

    When that happens then then all the (public only?) methods belonging to that class will be open to be considered for interception and thats the reason why methods like toString(), hashCode() will also be evaluated for interception.

    For eg: If you see your logs and if you are using the Regexp Pointcut Advisor you will see entries such as

    DEBUG [2006-05-25 17:23:00,301] [main] (JdkRegexpMethodPointcut.java:64) - Candidate is [java.lang.Object.hashCode]; pattern is [.*]; matched=true

    Now this does not mean that the toString method is being invoked, it is only being evaluated for interception against the regular expression. If it matches the regular expression though and if it is called anytime then be certain that the call will be intercepted.

    HTH,

  4. #4
    Join Date
    Apr 2008
    Posts
    2

    Default

    I have faced same problem. Configuration is like:
    Code:
    <aop:after-returning method="afterReturningMethod" pointcut="execution(* SomeInterface.*(..))"/>
    When toString() method is executed on bean instance then pointcut is matched! But this is true only for lowest-level interface. In the case I use some descendant interface
    Code:
    interface SomeDescendantInterface extends SomeInterface
    and change config to
    Code:
    <aop:after-returning method="afterReturningMethod" pointcut="execution(* SomeDescendantInterface.*(..))"/>
    problem is disappearing!

  5. #5
    Join Date
    Apr 2008
    Posts
    2

    Default

    I have finally understood this not obvious feature. Here is text from aspectj docs provided an explanation:

    When matching method-execution join points, if the execution pointcut method signature specifies a declaring type, the pointcut will only match methods declared in that type, or methods that override methods declared in or inherited by that type. So the pointcut

    Code:
      execution(public void Middle.*())
    picks out all method executions for public methods returning void and having no arguments that are either declared in, or inherited by, Middle, even if those methods are overridden in a subclass of Middle. So the pointcut would pick out the method-execution join point for Sub.m() in this code:

    Code:
     class Super {
        protected void m() { ... }
      }
      class Middle extends Super {
      }
      class Sub extends Middle {
        public void m() { ... }
      }
    My "toString()" issue was happening due to overriden "toString()" in class that is implementing lowest-level interface.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •