This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Use Spring AOP for Auditing?

 
Phoenix Kilimba
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, using reflection would work.

I'm still not convinced I wouldn't just create an "Auditable" interface, but like I said--whatever works :)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic