Skip to content

Commit fac31f5

Browse files
committed
Quick-and-dirty coverage of event handling the documentation
1 parent ba76226 commit fac31f5

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

src/docbkx/reference/mapping.xml

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ public class Person {
128128
</para>
129129

130130
<example>
131-
<title>Child object referred to using a DBRef</title>
132-
133131
<programlisting language="java"><![CDATA[
134132
@Document
135133
public class Account {
@@ -171,7 +169,45 @@ public class Person {
171169
<title>Handling Mapping Framework Events</title>
172170

173171
<para>Built into the MongoDB mapping framework are several <classname>org.springframework.context.ApplicationEvent</classname>
174-
events that your application can respond to
172+
events that your application can respond to by registering special beans in the <code>ApplicationContext</code>.
173+
</para>
174+
175+
<para>To intercept an object before it goes through the conversion process (which turns your domain object
176+
into a <classname>com.mongodb.DBObject</classname>), you'd register a subclass of <classname>org.springframework.data.document.mongodb.mapping.event.AbstractMappingEventListener</classname>
177+
that overrides the <code>onBeforeConvert</code> method. When the event is dispatched, your listener will be
178+
called and passed the domain object before it goes into the converter.
179+
</para>
180+
181+
<example>
182+
<programlisting language="java"><![CDATA[
183+
public class BeforeConvertListener<Person> extends AbstractMappingEventListener {
184+
@Override
185+
public void onBeforeConvert(Person p) {
186+
... does some auditing manipulation, set timestamps, whatever ...
187+
}
188+
}
189+
]]></programlisting>
190+
</example>
191+
192+
<para>To intercept an object before it goes into the database, you'd register a subclass of
193+
<classname>org.springframework.data.document.mongodb.mapping.event.AbstractMappingEventListener</classname>
194+
that overrides the <code>onBeforeSave</code> method. When the event is dispatched, your listener will be
195+
called and passed the domain object and the converted <classname>com.mongodb.DBObject</classname>.
196+
</para>
197+
198+
<example>
199+
<programlisting language="java"><![CDATA[
200+
public class BeforeSaveListener<Person> extends AbstractMappingEventListener {
201+
@Override
202+
public void onBeforeSave(Person p, DBObject dbo) {
203+
... change values, delete them, whatever ...
204+
}
205+
}
206+
]]></programlisting>
207+
</example>
208+
209+
<para>Simply declaring these beans in your Spring ApplicationContext will cause them to be invoked whenever the
210+
event is dispatched.
175211
</para>
176212
</section>
177213
</section>

0 commit comments

Comments
 (0)