File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Aspect Oriented Programming and the fly likes Is there another option for Spring AOP Annotations? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Aspect Oriented Programming
Bookmark "Is there another option for Spring AOP Annotations?" Watch "Is there another option for Spring AOP Annotations?" New topic
Author

Is there another option for Spring AOP Annotations?

Raistlen Majere
Greenhorn

Joined: Aug 03, 2009
Posts: 13
I apologize if the following doesn’t follow standard, but my Architect pretty much has dictated this direction.

The requirement is that every application should have the ability to log whatever it wants without the Developer having to worry about AOP. Is this the best approach, prob not but the Architect is concerned about annotation overhead and the responsibility of the developer to create annotations, thus I’m stuck with this approach.

Easy enough, I created a logging interface that pretty much just has a writeMessage(String, String). The policy will be any app that wants to log will need to implement this interface, thus implementing the dummy writeMessage method.

So my Foo class will call a doSomething() method that will have the ability to write a message passing in the arguments.

My plan is to implement an Aspect to monitor that writeMessage and do the actual writing based on the Aspects injected “writer” object which for now is just a simple write to screen.

My question is there an way to implement this strategy without using annotations but also without creating a separate proxy for each App?
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2343
    
  28

I might not be understand your post. Why do you need AOP for logging? Can't you just use JDK logging?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Jayesh A Lalwani wrote:I might not be understand your post. Why do you need AOP for logging? Can't you just use JDK logging?


Because AOP is actually a real elegant solution for logging. If you just use JDK Logging, then all that logging code is scattered throughout your code making your code lose its cohesion. And also makes your code look cleaner, which makes it easier to maintain, and would be less code. Imagine 2000 lines of logging code throughout your application. With AOP, that could be 3 lines of code in an aspect class and 10 lines of xml configuration.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Yes, you can configure Spring AOP with xml instead of Annotations.

But I will say this, I am calling out your Architect as having no knowledge of Annotations and what they truly mean.

"concerned about annotation overhead and the responsibility of the developer to create annotations" (Removed rude comment I made, I should call someone an idiot. I already know there are no idiots, just that we aren't seeing your Architect's expertise here.)

Mark
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

Here's my 2c

The requirement is that every application should have the ability to log whatever it wants without the Developer having to worry about AOP.

AOP won't give you to ability to log anything! It would make logging certain aspects of your application easy. A method entry, method exit, exceptions ....

I created a logging interface that pretty much just has a writeMessage(String, String). The policy will be any app that wants to log will need to implement this interface, thus implementing the dummy
writeMessage method


If you create an interface with a method and every class has to implement this interface and call this method when it needs to log (you said the aspect will monitor this method!) that kind of beats the purpose of using AOP in the first place.

Annotations


While I agree with Mark that your architect is making unreasonable statements regarding annotation - I personally won't see a good use case to use Annotation for logging using AOP.
In fact an aspect written with Aspect-J (configured in an XML) and used across all applications would be my best bet.

Do a bit googling for Using AOP for logging - you should get some pretty good samples, using AOP for logging is a pretty common use case.


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

Joined: Feb 05, 2001
Posts: 17250
    
    6

Sam, I would say that if you have to log elsewhere, in the middle of your method, then your method has low cohesion and is doing more than one thing. So for me the logging in the middle of a method is a huge code smell.

Also, with AOP before and after, I can get full context of what is being called so I can put everything I need of information logged in my AOP logger. Done right, Using AOP with creating your own @Logging() annotation is the easiest most elegant solution you can find for Logging. I do it all the time and it has made my code that much better.

Mark
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

Mark Spritzler wrote:Sam, I would say that if you have to log elsewhere, in the middle of your method, then your method has low cohesion and is doing more than one thing. So for me the logging in the middle of a method is a huge code smell.


That's a fair statement. Unfortunately I have to work we code that's not completely low cohesion and obviously does a lot more than it should. I don't write all the code - lots of people do - so it's difficult to keep the entire code base at the high standard!


Mark Spritzler wrote:
Also, with AOP before and after, I can get full context of what is being called so I can put everything I need of information logged in my AOP logger. Done right, Using AOP with creating your own @Logging() annotation is the easiest most elegant solution you can find for Logging. I do it all the time and it has made my code that much better.

Mark


I don't think its a bad idea - though I personally would prefer using XML to do the configuration. Simple reason is that if any new method is added - no one accidentally forgets to add the @Logging annotation. I like to leave as few possibility for people accidentally forgetting to do something!

Like I said, just a personal preference.

Mark Spritzler wrote:
"concerned about annotation overhead and the responsibility of the developer to create annotations" (Removed rude comment I made, I should call someone an idiot. I already know there are no idiots, just that we aren't seeing your Architect's expertise here.)
Mark


You probably meant "I shouldn't call someone..."
Actually going over the statement over again the architect might have a valid point. All he/she could be saying is that it would be an overhead to make sure that developers add the @annotation on every method. I think that's fair statement!
Of course if he/she meant performance overhead - then he/she is definitely off the track!

Mark Spritzler wrote:
I already know there are no idiots

I wouldn't be so sure about that
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

don't think its a bad idea - though I personally would prefer using XML to do the configuration. Simple reason is that if any new method is added - no one accidentally forgets to add the @Logging annotation. I like to leave as few possibility for people accidentally forgetting to do something!


True, and I could counter with the fact that if a developer doesn't know that a pointcut expression exists or understands it in xml, the might want logging but not write the signature in a way that it matches the pointcut expression and accidentally doesn't get logging on that method. Whereas, if they are in the method they can see if the annotation is there or not. If it is, then they know it is being logged, if not, it isn't being logged. With xml, I would have to go back and forth between the code and the xml and some small brain power to figure it out.

Yes, I really meant "shouldn't" Thanks for pointing that out.

But, I really do find that even though some people are more "idiotic" than others, each and every one of us has something that we know more than the other person.
At least one subject or task. Like I have no social skills. So I might be great at math or programming, I am an idiot in social settings. So I take the half full approach and say there are no idiots, just not seeing their expertise at the moment.

Mark

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there another option for Spring AOP Annotations?