Announcement Announcement Module
Collapse
No announcement yet.
AOP within methods Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP within methods

    Hi Spring community.

    I want to enable aop by logging all methods calls. I want to log all method calls including the one that is currently called by the proxy.

    So let's say i have a bean BeanX and a method interceptor LogBeanY, i want that all the method call including self method invocation be reported in the log, so how i will configure my spring xml ?

    Here is the output i'd like to have by calling the method doStuffA of the BeanX:

    OUTPUT
    entering method doStuffA
    executing doStuffA
    entering method doStuffB
    executing doStuffB
    entering method doStuffC
    executing doStuffC
    exiting method doStuffC
    exiting method doStuffB
    exiting method doStuffA

    ACTUALLY, the result i have with spring xml configuration below is :
    entering method doStuffA
    executing doStuffA
    executing doStuffB
    executing doStuffC
    exiting method doStuffA

    Code:
    public class LogBeanY implementsMethodInterceptor {
    
    	public LogBeanY () {
    	}
    
    	public Object invoke(MethodInvocation invocation) throws Throwable {
    			
    			Object object = null;
    		
    			System.out.println("entering method " +                          invocation.getMethod().getName());
    
    			object = invocation.proceed();
    			
    			System.out.println("exiting method " +                            invocation.getMethod().getName());
    			
    			return object;
    	}
    }
    
    public class BeanX {
      public BeanX() {}
    
      public void doStuffA() {
       System.out.println("executing doStuffA");
       this.doStuffB();
     }
    
     public void doStuffB() {
       System.out.println("executing  doStuffB");
       this.doStuffC();
     }
    
     public void doStuffC() {
       System.out.println("executing  doStuffC");
     }
    
    };
    SPRING XML :
    <bean id="bean.x" class="BeanX"/>
    <bean id="bean.log.y" class="LogBeanY"/>

    <aop:config>
    <aopointcut id="pointcut.method"
    expression="execution(* BeanX.*(..))" />
    <aop:advisor advice-ref="bean.log.y"
    pointcut-ref="pointcut.method" />
    </aop:config>


    IS THERE ANYBODY THAT CAN HELP ME WITH THIS ???
    THANKS VERY MUCH

  • #2
    Once the target object (BeanX) is reached through the proxy object then self invocation will not pass through the proxy.

    If u require the output what specified in the forum then u need to use AspectJ.

    Please read http://static.springsource.org/sprin...rence/aop.html

    From the above link
    HTML Code:
    The key thing to understand here is that the client code inside the main(..) of the Main  class has a reference to the proxy. This means that method calls on that object reference will be calls on the proxy, and as such the proxy will be able to delegate to all of the interceptors (advice) that are relevant to that particular method call. However, once the call has finally reached the target object, the SimplePojo reference in this case, any method calls that it may make on itself, such as this.bar() or this.foo(), are going to be invoked against the this reference, and not the proxy. This has important implications. It means that self-invocation is not going to result in the advice associated with a method invocation getting a chance to execute.

    Comment


    • #3
      Originally posted by Bru View Post
      ...
      IS THERE ANYBODY THAT CAN HELP ME WITH THIS ???
      THANKS VERY MUCH
      You just encountered the most popular spring aop problem
      I described it here - Spring AOP top problem #1 - aspects are not applied.

      The best solution from my point of view is aspectj usage. Feel free to check the example here - Weaving with AspectJ

      Comment

      Working...
      X