Results 1 to 8 of 8

Thread: calling internal methods is not working

  1. #1
    Join Date
    Jan 2008
    Posts
    2

    Default calling internal methods is not working

    Hi,

    I am using springframework and i am trying to define some pointcut which doesn't work!!
    my Advice is not called....

    I have the following pointcut:
    <aop: pointcut id="testPointCut" expression="execution(* RetrieveDeviceInfoDL.doService(java.lang.Object))" />



    RetrieveDeviceInfoDL.java:
    Code:
    class RetrieveDeviceInfoDL extends ExternalSystemDataLayerBase {
           protected Object doService(Object input) throws ApplicationException
    	{
                     //....
                 }
    }
    ExternalSystemDataLayerBase.java:
    Code:
    public abstract class ExternalSystemDataLayerBase implements ExternalSystemDataLayer {
    	public final Object getData(DataObject input)
    	{
    		Object responseObject = doService(convertToExternal(input));
    		return responseObject;
    	}
    }
    ExternalSystemDataLayer.java:
    Code:
    public interface ExternalSystemDataLayer extends DataLayer
    {
    	public Object getData(DataObject aInputData);
    }

    This is NOT working!!!

    however,
    if i change the pointcut to the following (a thing that i DO NOT want!!) it works:
    <aop: pointcut id="testPointCut" expression="execution(* ExternalSystemDataLayerBase.getData(java.lang.Obje ct))" />



    Any ideas?
    is there a problem to define a pointcut on a methid that is called "internally" from within the same instance?

    Please advice/help ASAP.

  2. #2
    Join Date
    Jan 2008
    Location
    Mohnton, PA USA (that's near Philadelphia)
    Posts
    2,148

    Default

    Keep in mind:
    Spring AOP is Proxy based (uses Java Dynamic Proxies) which in your case means that when you retrieve an instance of ExternalSystemDataLayer (interface) what you actually get back is a Proxy which wraps your inheritance structure.
    So when you are getting ExternalSystemDataLayer from the ApplicationContext you get Proxy and all the calls to your methods are proxied , however internally (inside of Proxy) you are calling a method which is not proxied.
    Spring AOP does not do any code weaving. If you really want to achieve that level of granularity use AspectJ or other AOP frameworks (i.e., JBoss AOP) which do code weaving (load or compile time)

    Hope it explains.

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    As explained in chapter 6.5.3 of the reference guide spring aop is a proxy based implementation of aop. Which means only public calls INTO the object (external method calls) can be intercepted.

    You have 2 issues here
    1. Internal method call
    2. protected method call.

    If you want to intercept internal method calls you need to use AspectJ (for instance) and loadtime or compile time weaving.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Jan 2008
    Location
    Mohnton, PA USA (that's near Philadelphia)
    Posts
    2,148

    Default

    1. Internal method call
    True
    2. protected method call.
    Not True

    That was a surprise for me as well (just tested it). Protected method was intercepted when called on the Proxy.
    Now, the caviat is that in my test it is a CGLIB proxy which means that I am not implementing any interfaces. Obviously if I did, then I would have to cast my Proxy to the Interface, thus would have no access to any methods other then public methods defined in my Interface.

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    In the situation mentioned above those 2 situations are correct. He implements interfaces thus JDK Dynamic Proxies are used. I was talking about HIS specific situation.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6
    Join Date
    Jan 2008
    Location
    Mohnton, PA USA (that's near Philadelphia)
    Posts
    2,148

    Default

    I agree,
    I actually didn't realize that myself until tested it, but it does make perfect sense.
    Cheers

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    It does for a class proxy because a public/protected/default access method can still be called externally (be it from within the same package but it still is possible).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  8. #8
    Join Date
    Jan 2008
    Posts
    2

    Default

    Thanks guys
    that was helpful

Posting Permissions

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