• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is there another option for Spring AOP Annotations?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might not be understand your post. Why do you need AOP for logging? Can't you just use JDK logging?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic