Results 1 to 10 of 11

Thread: Schema-based AOP: advice not executed

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    Germany
    Posts
    14

    Default Schema-based AOP: advice not executed

    Hello,

    I am going nuts: I am new and just want to implement a simple profiler within my app that is basically based on that from the Spring docs.

    First:
    - I use Java 1.5
    - Spring is 2 RC3
    - I regard pointcuts as part of my config, so they reside within my XML file
    - all my implementations are based on interfaces
    - features of AspectJ not required

    So, I just want all methods of my implementations to cause invokation of a profiler method. My config looks like this:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
    	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	   xmlns:aop="http://www.springframework.org/schema/aop"
    	   xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    	<bean
    		id="profiler"
    		class="my.package.SimpleProfiler"
    	/>
    
    	<aop:config>
    		<aop:aspect id="profiling" ref="profiler">
    		    <aop:around 
          			pointcut="execution(* my.package.*.*(..))" 
          			method="profile"
          		/>
    		</aop:aspect>
    	</aop:config>
    
    [...]
    
    </beans>
    This is the SimpleProfiler implementation:
    Code:
    package my.package;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.springframework.util.StopWatch;
    
    public class SimpleProfiler {
    
    	public Object profile(ProceedingJoinPoint call) throws Throwable {
    		System.out.println("Profiler");
    	      StopWatch clock = new StopWatch(
    	            "Profiling...");
    	      try {
    	         clock.start(call.toShortString());
    	         return call.proceed();
    	      } finally {
    	         clock.stop();
    	         System.out.println(clock.prettyPrint());
    	      }
    	   }
    
    }
    As said, straight from the Spring docs. However, whatever pointcuts I try, nothing happens, although I invoke methods from my implementations - all created by the Spring container. Additionally, the AOP config is parsed; so if I do errors in aop: elements or their attribute names, I get an exception.

    Do I have to activate AOP somewhere? I have read the docs and forums quite some times but fail to get a solution. Also, as you see my pointcut is quite general - do I have to name interfaces there? Although I have interfaces, I do not know if this bytecode modification stuff applies here...

    Thanks and regards,
    Timo

    P.S.: this is no web-app but a simple standalone app launched from a shell

  2. #2
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    The code itself seems alright. The only suspect is the pointcut itself (and due to removed code for bean definitions, it is not possible to give a definitive answer).

    My suggestion is that you relax the pointcut definition to "execution(* *(..))". This will select more join points than you need, but it will give a good starting point.

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  3. #3
    Join Date
    Aug 2006
    Location
    Germany
    Posts
    14

    Default

    Hello ramnivas,

    Thanks for the reply. I forgot to mention that this was exactly what I have tried before posting, namely to relax pointcut expressions. But good to know that code seems alright.

    Thanks and regards,
    Timo

  4. #4
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    Perhaps, you can post a small complete project to help track down the problem.

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  5. #5
    Join Date
    Aug 2006
    Location
    Germany
    Posts
    14

    Default

    Rehi,

    Well, I would but it is really hard to prepare, because I modularised it into several Jars that I would have to decompose.

    But perhaps another question of design - I had this in mind in order to use AOP:

    There is a central class instance in my application that ALL objects need a reference to - and that before any methods or properties of them are called/invoked.

    So, one solution would be by using Spring IoC: for every bean created, I could set that instance using a property (so a ref to that central object). Tradeoffs:
    - I have to to that property for every bean, much redundancy
    - access order problem: setters of these beans invoked through the IoC container already require access to central object and thus fail (null pointer)

    The other solution would be to use Spring IoC again, now providing access to this central instance by passing it to each bean as constructor argument. Tradeoffs:
    - Again, too much redundancy
    - My app implements interfaces and as these constructors become vital part of the inner workings, it is not nice that I cannot declare these required constructors in my interface declarations, e.g. public AnyBean(Object centralObj);

    Is AOP the right solution for this kind of automatism? In my view, it is a central aspect that each bean receives a reference to a central object just after their creation.
    But I suppose, there is the problem that I cannot declare pointcuts for constructors, or? So, this aspect would always have to execute exactly after each constructor of any object in order to call a setter publishing my central implementation...

    I hope I made my problem clear ;-)

    Thanks and regards,
    Timo

  6. #6
    Join Date
    Sep 2004
    Location
    Texas
    Posts
    155

    Default

    Timo, I wonder if a mixin interface would be the easiest solution to your problem. Suppose you have an interface:

    Code:
    public interface CentralObjectLocator()
    {
       CentralObject getCentralObject();
    }
    If I remember correctly, you can use the pointcut language to mix this interface into a large swath of your services at once. Only the implementation of this interface would have to be injected with CentralObject. This would keep you from having to configure each and every service to be injected with CentralObject.
    Corby

Posting Permissions

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