aspose file tools*
The moose likes Spring and the fly likes How to Use Spring AOP for Auditing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to Use Spring AOP for Auditing?" Watch "How to Use Spring AOP for Auditing?" New topic
Author

How to Use Spring AOP for Auditing?

Phoenix Kilimba
Ranch Hand

Joined: Oct 10, 2006
Posts: 64
Dear Sirs et Madames;

I have an application in which there are a number of persistent entities. All these entities have a few things in common, they all have the fields insertedBy, insertedDate.

The insertedBy field is the currently logged in user. My Dao interface looks like so:



and my EntityCrud Interface looks like so:


EntityCrudImpl looks like:


My question is: I need the two properties of an entityItem insertedBy, insertedDate updated whenever the create() method (which effectively is the method which persists the entityItem) is called. I got it into my head to use Spring AOP and to create an aspect to achieve this, however as am kind of new to Spring AOP could anyone tell me how this could be achieved, and the merits of using aspects to achieve it? Is there an example where one has used aspects for auditing which I could maybe have a look at?

Any help would be greatly appreciated.


You are what you know
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Too much code, too many comments: particular when asking non-specific questions, post *only* what is specifically necessary to provide information useful to answering the question.

Why do you want an aspect for this? Why not just code it somewhere?
Phoenix Kilimba
Ranch Hand

Joined: Oct 10, 2006
Posts: 64
Sorry about the code that goes on and on, I tried just coding it, however, ALL persistent entities have these two fields, but not all persistent entities share a parent superclass. So the problem becomes, should I hard code this logic directly into each entityDao (repetitive, always the same code) or is there someway I can centralise the code to avoid repetitiveness (Inheriting from a superclass would be the first choice but that forces some classes which dont always fit the IS-A criteria being forced to inherit from a superclass) , so I thought Aspects may allow me to write once and call where needed. Hope I explained that well enough, please let me know if you need any more info.

Thanks again in advance
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Have them implement an interface.

I'm not saying not to do it with aspects, but you'd have the same problem with the class hierarchy unless you had as aspect for each type. At that point the complexity of an AOP solution outweighs just doing it normally.
Phoenix Kilimba
Ranch Hand

Joined: Oct 10, 2006
Posts: 64
David Newton wrote:Have them implement an interface.

I'm not saying not to do it with aspects, but you'd have the same problem with the class hierarchy unless you had as aspect for each type. At that point the complexity of an AOP solution outweighs just doing it normally.


Well correct me if am wrong, but I thought I could avoid having an aspect for each type if I had the create() method in EntityCrudImpl as the join point. Would that not avoid the repetitious code, would that not work?

Thanks again
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I thought you were saying that you didn't have a consistent interface to get the values to update--how would you call the setters for update date etc. from the aspect?
Phoenix Kilimba
Ranch Hand

Joined: Oct 10, 2006
Posts: 64
Sorry for the late response, I had left the office. Well the Aspect class looks like so:



And is configured in applicationContext.xml like so:



Any observations or criticisms greatly appreciated, all part of the learning process :-) !
Thanks again
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Sure, using reflection would work.

I'm still not convinced I wouldn't just create an "Auditable" interface, but like I said--whatever works :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Use Spring AOP for Auditing?