This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Aspect Oriented Programming and the fly likes Is Spring AOP really an effective strategy? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Aspect Oriented Programming
Bookmark "Is Spring AOP really an effective strategy?" Watch "Is Spring AOP really an effective strategy?" New topic
Author

Is Spring AOP really an effective strategy?

Raistlen Majere
Greenhorn

Joined: Aug 03, 2009
Posts: 13
I'm very new to Spring AOP so this might just be something I've over looked. The situation is our application wants a sound Logging strategy that eliminates the complexity from the responsibility of the Developer. Spring AOP seems to do that except for one issue. We have a multi tier application with what could be 100's of apps wired into the execution of our program. If any of those apps would all of a sudden want to write a specific message, does the Developer now have to change the aspect and then re-wire the xml? Also, I'm assuming every single APP would have to be wired through the xml in the form of proxies right?

If an APP needs to write a specific message anyway, wouldn't it make more since to just take care of it within the app? By doing this through AOP, this would seem to make our "base" wiring xml to be just enormous with every single app registered?

Is there a simple way around this? I know I wouldn't have to register each method, but the apps still would need proxies set up right?

AOP makes since for a trace type log strategy but I can't quite seem to see the benefit for a complete logging strategy.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

OK, I got confused in reading what exactly you are looking for or what would cause you issues.

But, I think I still might have an elegant solution for you.

AOP is a perfect strategy for all Logging.

You could create your own Annotation that you has an attribute for the message you want printed. Then create your Pointcut to match methods with that annotation, and you can pass that annotation on that method into your aop advice method, and your aop advice method would be generic enough to handle new messages, new applications, or anything.

Hope that is what you meant and that I helped.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Raistlen Majere
Greenhorn

Joined: Aug 03, 2009
Posts: 13
I think you may be right on target but I will have to do some more research.

If I have 100 apps, and everyone of their execute methods needs to log "I'm Executing!" (just an example), do all 100 apps need proxies set up in spring?

Thanks for your reply and I will look into annotations.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

Raistlen Majere wrote:I think you may be right on target but I will have to do some more research.

If I have 100 apps, and everyone of their execute methods needs to log "I'm Executing!" (just an example), do all 100 apps need proxies set up in spring?

Thanks for your reply and I will look into annotations.


Still a bit confused 100 apps each of them Spring apps?

I don't understand "do all 100 apps need proxies set up in spring?"

Do you mean all different .war files each starts up their own Spring container, each with their own configuration

of

Do you mean one Spring application with over 100 beans in it.

In any case, if you want AOP there are three mechanisms to get that code to log. 1) Dynamic Proxies at runtime based on bean's interface. 2) CGLIB jar in your classpath, create proxies at runtime by extending your class. 3) AspectJ running ajc after compiling will add the code through byte code instrumentation, meaning the code is injected into you .class as if you had coded it there.

Now about proxies, I am sensing you think that proxies are a bad thing. Maybe that you think it slows things down. I can guarantee you, by experience and by actually timing things, proxies will not make a difference to your application, especially one that just logs a message. And that many servers out there use these Dynamic Proxy pattern themselves, like all the app servers. If it was a bad idea, all of them wouldn't be using it.

Hope that helps.

Mark
Raistlen Majere
Greenhorn

Joined: Aug 03, 2009
Posts: 13
I apologize for confusion. It would be more like a hundred beans injected to create a spring app. Within those beans I would need to monitor anytime lets say a writeMessage(String strInput) was called.

I was hoping there was a way to just monitor the method itself without setting up a proxy for all 100 beans. In other words, make the Spring xml file easier to manage.

I don't think proxies are a bad thing, I just think having to create 100 of them to monitor the same method is.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Raistlen Majere wrote:
I was hoping there was a way to just monitor the method itself without setting up a proxy for all 100 beans. In other words, make the Spring xml file easier to manage.


You don't have to setup proxies for 100 of beans - you just need to configure your logging aspect in Spring XML and spring will create the proxies behind the scene (and any solution which has the two words Spring and AOP will have Proxies involved!!)

To be frank - I think you need to understand AOP before you try coding with it.

Spring provides a free online AOP training that I think is really good - give it a try


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

To add to Sam's stuff. Based on our pointcut expression, Spring will only proxy those beans that have methods that match the expression. If a bean doesn't have that method, then no proxy. For the methods that don't match that are also in the bean that has one method that matches, the proxy does nothing when calling those methods, it just goes directly to the bean and calls that method.

But to re-iterate what Sam said, you as a developer never creates a Proxy, Spring does that for you.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is Spring AOP really an effective strategy?