PDA

View Full Version : AOP to generate ApplicationEvents


khagel
Nov 11th, 2004, 12:34 PM
We have employees, for example. EmployeeDAO and the standard hibernatetemplate stuff with OpenSessionInViewInterceptor. It's all working fine. The "owned" objects (declared in the mapping) are cascade deleted successfully.
As we add new subsystems to the application, we find other domain objects which refer to an association with an employee. We would like to be able to add such subsystems without having to alter the existing mapping of the Employee - we're using XDoclet to generate those by the way. The Employee doesn't own these other objects (object graphs), doesn't own the associations, to be exact doesn't even know they exist.

Some of these other systems are interested in Employee delete events. I may not have access to the EmployeeDAO code, so I can't just add new methods to it to generate or publish events. Is AOP an answer here? Can I intercept delete methods in various DAOs and have them publish EmployeeDeleteApplicationEvents, so to speak? Is this a path worth pursuing, does anybody here have experience with similar situations?

I don't see a lot in the docs about ApplicationEvents, I would also appreciate any advice as to their successful implemention in Spring.

Thanks,
K

Ben Alex
Nov 11th, 2004, 04:34 PM
Yes you could intercept delete methods on your DAOs and publish an ApplicationEvent. We use ApplicationEvent extensively within Acegi Security. It's pretty straight-forward, it just works.

In relation to your interceptor, the easiest way is to write an implementation of MethodInterceptor, extend Spring's StaticMethodMatcherPointcutAdvisor, and use a DefaultAdvisorAutoProxyCreator in your application context. I posted some source code for a similar DAO advisor and interceptor at http://forum.springframework.org/showthread.php?t=11293. You'd just need your MethodInterceptor to implement ApplicationContextAware to get hold of the ApplicationContext so it can applicationContext.publishEvent(yourEvent).