wood burning stoves 2.0*
The moose likes Aspect Oriented Programming and the fly likes Spring AOP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Aspect Oriented Programming
Bookmark "Spring AOP" Watch "Spring AOP" New topic
Author

Spring AOP

nitinram agarwal
Ranch Hand

Joined: Jan 29, 2009
Posts: 83
Hello,
I have the following situation

Some main program calls the method doSomeOperation which in turn calls the other methods in the class based on business rule. I have a situation where I have to populate some stats after some of the methods in this class is invoked.

For example, after calling doSomeOperation which in turn calls say doOps1, populate some stats table on the database indicating how many records were inserted/updated/deleted etc in specific table and how much time it took etc by doOps1 method. I am trying to use Spring AOP for this purpose. However the issue that I am facing is that the intended code is not getting invoked.



The Spring bean definition



On running the main program, I am getting the output (and it is not calling profile1 rom the Aspect class - BusinessProfiler). however if I directly call the doOps1 from main class then the aspect method gets invoked. I would like to know if aspect is supposed to work if only called from the main method and not otherwise.




Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

The problem is that by default Spring uses interface based proxies. Meaning to call the method through the proxy you must do it through the interface. You have a self call that is not done through the proxy but on a reference of this


You could try proxying the target class by adding that option to your <aop:aspectj-autoproxy /> tag



or you could also look at different weaving options. There is a blog I found that talks about this a little bit

http://denis-zhdanov.blogspot.com/2009/07/spring-aop-top-problem-1-aspects-are.html

The Spring reference documentation also has a lot of material on proxy choices.


[How To Ask Questions][Read before you PM me]
nitinram agarwal
Ranch Hand

Joined: Jan 29, 2009
Posts: 83
This did not solve the problem. I am reading the documentation to understand the finer details.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

I had my doubts the proxy target class would work, but you did not experiment with other weaving options.

Now if you expose doOps1 via the interface and access it through that interface it will work

i.e.



If you want your original code to work as you posted it originally with the self call, then weave in your aspects at compile time, then it will work. This can be done with the maven plugin see the below POM fragment for an example.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring AOP