Merge pull request #241 from alex-semenyuk/master

Added simple UserCascadeSave email for User
This commit is contained in:
Eugen 2015-08-23 12:59:04 -07:00
commit 7036c278d2
2 changed files with 24 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import java.util.List;
import org.baeldung.converter.UserWriterConverter;
import org.baeldung.event.CascadeSaveMongoEventListener;
import org.baeldung.event.UserCascadeSaveMongoEventListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
@ -36,6 +37,11 @@ public class MongoConfig extends AbstractMongoConfiguration {
return "org.baeldung";
}
@Bean
public UserCascadeSaveMongoEventListener userCascadingMongoEventListener() {
return new UserCascadeSaveMongoEventListener();
}
@Bean
public CascadeSaveMongoEventListener cascadingMongoEventListener() {
return new CascadeSaveMongoEventListener();

View File

@ -0,0 +1,18 @@
package org.baeldung.event;
import org.baeldung.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
public class UserCascadeSaveMongoEventListener extends AbstractMongoEventListener<Object> {
@Autowired
private MongoOperations mongoOperations;
@Override
public void onBeforeConvert(final Object source) {
if (source instanceof User && ((User) source).getEmailAddress() != null) {
mongoOperations.save(((User) source).getEmailAddress());
}
}
}