DATADOC-62 - Create abstract @Configuration class for Mongo Configuration

This commit is contained in:
Mark Pollack
2011-04-06 18:21:51 -04:00
parent 40d9930b2c
commit 9fe27e590d
6 changed files with 146 additions and 7 deletions

View File

@@ -0,0 +1,96 @@
/*
* Copyright 2010-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.document.mongodb.config;
import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.convert.MappingMongoConverter;
import org.springframework.data.document.mongodb.mapping.Document;
import org.springframework.data.document.mongodb.mapping.MongoMappingContext;
import org.springframework.data.document.mongodb.mapping.MongoPersistentEntityIndexCreator;
import org.springframework.data.document.mongodb.mapping.event.MappingEventListener;
import org.springframework.data.document.mongodb.mapping.event.MongoMappingEvent;
import org.springframework.data.mapping.context.MappingContextAwareBeanPostProcessor;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import com.mongodb.Mongo;
@Configuration
public abstract class AbstractMongoConfiguration {
@Bean
public abstract Mongo mongo() throws Exception;
@Bean
public abstract MongoTemplate mongoTemplate() throws Exception;
public String getMappingBasePackage() {
return "";
}
@Bean
public MappingEventListener<MongoMappingEvent> mappingEventsListener() {
return new MappingEventListener<MongoMappingEvent>();
}
@Bean
public MongoMappingContext mongoMappingContext() throws ClassNotFoundException, LinkageError {
MongoMappingContext mappingContext = new MongoMappingContext();
String basePackage = getMappingBasePackage();
if (StringUtils.hasText(basePackage)) {
ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider(false);
componentProvider.addIncludeFilter(new AnnotationTypeFilter(Document.class));
componentProvider.addIncludeFilter(new AnnotationTypeFilter(Persistent.class));
Set<Class<?>> initialEntitySet = new HashSet<Class<?>>();
for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {
initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(), mappingContext.getClass().getClassLoader()));
}
mappingContext.setInitialEntitySet(initialEntitySet);
}
return mappingContext;
}
@Bean
public MappingMongoConverter mappingMongoConverter() throws Exception {
MappingMongoConverter converter = new MappingMongoConverter();
converter.setMappingContext(mongoMappingContext());
converter.setMongo(mongo());
return converter;
}
@Bean
public MappingContextAwareBeanPostProcessor mappingContextAwareBeanPostProcessor() {
MappingContextAwareBeanPostProcessor bpp = new MappingContextAwareBeanPostProcessor();
bpp.setMappingContextBeanName("mongoMappingContext");
return bpp;
}
@Bean MongoPersistentEntityIndexCreator mongoPersistentEntityIndexCreator() throws Exception {
MongoPersistentEntityIndexCreator indexCreator = new MongoPersistentEntityIndexCreator(mongoMappingContext(), mongoTemplate() );
return indexCreator;
}
}

View File

@@ -14,17 +14,16 @@
* limitations under the License.
*/
package org.springframework.data.document.mongodb.mapping;
package org.springframework.data.document.mongodb.mapping.event;
import com.mongodb.DBObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.document.mongodb.mapping.event.AbstractMappingEventListener;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
public class MappingEventsListener<MongoMappingEvent> extends AbstractMappingEventListener {
public class MappingEventListener<MongoMappingEvent> extends AbstractMappingEventListener {
private Log log = LogFactory.getLog(getClass());

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2010-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.document.mongodb;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.document.mongodb.config.AbstractMongoConfiguration;
import com.mongodb.Mongo;
@Configuration
public class GeoSpatialAppConfig extends AbstractMongoConfiguration {
@Bean
public Mongo mongo() throws Exception {
return new Mongo("localhost");
}
@Bean
public MongoTemplate mongoTemplate() throws Exception {
return new MongoTemplate(mongo(), "geospatial", "newyork", mappingMongoConverter());
}
public String getMappingBasePackage() {
return "org.springframework.data.document.mongodb";
}
}

View File

@@ -29,6 +29,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.document.mongodb.geo.Box;
@@ -69,7 +70,7 @@ public class GeoSpatialTests {
for (String coll : collectionsToDrop) {
db.getCollection(coll).drop();
}
applicationContext = new ClassPathXmlApplicationContext("/geospatial.xml");
applicationContext = new AnnotationConfigApplicationContext(GeoSpatialAppConfig.class);
template = applicationContext.getBean(MongoTemplate.class);
template.setWriteConcern(WriteConcern.FSYNC_SAFE);
template.ensureIndex(new GeospatialIndex("location"));

View File

@@ -7,7 +7,7 @@
<mongo:mongo host="localhost" port="27017"/>
<mongo:mapping-converter base-package="org.springframework.data.document.mongodb.mapping"/>
<mongo:mapping-converter base-package="org.springframework.data.document.mongodb"/>
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
<constructor-arg ref="mongo"/>
@@ -16,6 +16,6 @@
<constructor-arg ref="mappingConverter"/>
</bean>
<bean class="org.springframework.data.document.mongodb.mapping.MappingEventsListener"/>
<bean class="org.springframework.data.document.mongodb.mapping.event.MappingEventListener"/>
</beans>

View File

@@ -15,6 +15,6 @@
<constructor-arg ref="mappingConverter"/>
</bean>
<bean class="org.springframework.data.document.mongodb.mapping.MappingEventsListener"/>
<bean class="org.springframework.data.document.mongodb.mapping.event.MappingEventListener"/>
</beans>