Added xml support for event listener and made minor cleanup

This commit is contained in:
alex-semenyuk 2015-08-15 16:34:42 +02:00
parent 236b808d05
commit 971d62568e
6 changed files with 27 additions and 24 deletions

View File

@ -3,9 +3,8 @@ package org.baeldung.config;
import java.util.ArrayList;
import java.util.List;
import org.baeldung.converter.UserWriterConverter;
import org.baeldung.event.CascadingMongoEventListener;
import org.baeldung.event.CascadeSaveMongoEventListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
@ -20,7 +19,7 @@ import com.mongodb.MongoClient;
@EnableMongoRepositories(basePackages = "org.baeldung.repository")
public class MongoConfig extends AbstractMongoConfiguration {
private List<Converter<?,?>> converters = new ArrayList<Converter<?,?>>();
private List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
@Override
protected String getDatabaseName() {
@ -36,15 +35,15 @@ public class MongoConfig extends AbstractMongoConfiguration {
public String getMappingBasePackage() {
return "org.baeldung";
}
@Bean
public CascadingMongoEventListener cascadingMongoEventListener(){
return new CascadingMongoEventListener();
public CascadeSaveMongoEventListener cascadingMongoEventListener() {
return new CascadeSaveMongoEventListener();
}
@Override
public CustomConversions customConversions() {
converters.add(new UserWriterConverter());
return new CustomConversions(converters);
converters.add(new UserWriterConverter());
return new CustomConversions(converters);
}
}

View File

@ -9,7 +9,7 @@ import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
public class CascadingMongoEventListener extends AbstractMongoEventListener<Object> {
public class CascadeSaveMongoEventListener extends AbstractMongoEventListener<Object> {
@Autowired
private MongoOperations mongoOperations;
@ -24,7 +24,7 @@ public class CascadingMongoEventListener extends AbstractMongoEventListener<Obje
final Object fieldValue = field.get(source);
if (fieldValue != null) {
DbRefFieldCallback callback = new DbRefFieldCallback();
FieldCallback callback = new FieldCallback();
ReflectionUtils.doWithFields(fieldValue.getClass(), callback);

View File

@ -5,7 +5,7 @@ import java.lang.reflect.Field;
import org.springframework.data.annotation.Id;
import org.springframework.util.ReflectionUtils;
public class DbRefFieldCallback implements ReflectionUtils.FieldCallback {
public class FieldCallback implements ReflectionUtils.FieldCallback {
private boolean idFound;
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {

View File

@ -8,12 +8,12 @@ public class EmailAddress {
@Id
private String id;
private String value;
public EmailAddress(){
public EmailAddress() {
}
public EmailAddress(String value) {
this.value = value;
this.value = value;
}
public String getId() {

View File

@ -20,17 +20,16 @@ public class User {
private String name;
private Integer age;
@DBRef
@Field("email")
@CascadeSave
private EmailAddress emailAddress;
public User(){
public User() {
}
public User(String name, Integer age, String value){
public User(String name, Integer age, String value) {
this.name = name;
this.age = age;
this.emailAddress = new EmailAddress(value);

View File

@ -10,21 +10,26 @@
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
>
<context:annotation-config/>
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="localhost"/>
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongo" ref="mongo"/>
<constructor-arg ref="mongoConverter" />
<constructor-arg name="databaseName" value="test"/>
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<mongo:repositories base-package="org.baeldung.repository" mongo-template-ref="mongoTemplate"/>
<bean class="org.baeldung.event.BeforeConvertListener">
<bean class="org.baeldung.event.CascadingMongoEventListener">
</bean>
<mongo:mapping-converter id="mongoConverter" base-package="org.baeldung.converter">
<mongo:custom-converters base-package="org.baeldung.converter"/>
</mongo:mapping-converter>
</beans>