Merge pull request #241 from alex-semenyuk/master
Added simple UserCascadeSave email for User
This commit is contained in:
commit
7036c278d2
|
@ -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();
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue