From fac31f5ce3ae1ee2ee64b8d145d39ef0f058f925 Mon Sep 17 00:00:00 2001 From: "J. Brisbin" Date: Fri, 1 Apr 2011 14:22:59 -0500 Subject: [PATCH] Quick-and-dirty coverage of event handling the documentation --- src/docbkx/reference/mapping.xml | 42 +++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/docbkx/reference/mapping.xml b/src/docbkx/reference/mapping.xml index b815dc065..e23f1c69f 100644 --- a/src/docbkx/reference/mapping.xml +++ b/src/docbkx/reference/mapping.xml @@ -128,8 +128,6 @@ public class Person { - Child object referred to using a DBRef - Handling Mapping Framework Events Built into the MongoDB mapping framework are several org.springframework.context.ApplicationEvent - events that your application can respond to + events that your application can respond to by registering special beans in the ApplicationContext. + + + To intercept an object before it goes through the conversion process (which turns your domain object + into a com.mongodb.DBObject), you'd register a subclass of org.springframework.data.document.mongodb.mapping.event.AbstractMappingEventListener + that overrides the onBeforeConvert method. When the event is dispatched, your listener will be + called and passed the domain object before it goes into the converter. + + + + extends AbstractMappingEventListener { + @Override + public void onBeforeConvert(Person p) { + ... does some auditing manipulation, set timestamps, whatever ... + } +} + ]]> + + + To intercept an object before it goes into the database, you'd register a subclass of + org.springframework.data.document.mongodb.mapping.event.AbstractMappingEventListener + that overrides the onBeforeSave method. When the event is dispatched, your listener will be + called and passed the domain object and the converted com.mongodb.DBObject. + + + + extends AbstractMappingEventListener { + @Override + public void onBeforeSave(Person p, DBObject dbo) { + ... change values, delete them, whatever ... + } +} + ]]> + + + Simply declaring these beans in your Spring ApplicationContext will cause them to be invoked whenever the + event is dispatched.