1、配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd"
default-lazy-init="true">
<!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 -->
<mongo:mongo host="${mongo.host}" port="${mongo.port}">
<mongo:options connections-per-host="800"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="80000"
max-wait-time="150000"
auto-connect-retry="true"
socket-keep-alive="true"
socket-timeout="150000"
slave-ok="true"
write-number="1"
write-timeout="1000"
write-fsync="true" />
</mongo:mongo>
<bean id="mongoMappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
<!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->
<mongo:db-factory id="factory" mongo-ref="mongo"
dbname="${mongo.dbname}" username="${mongo.username}" password="${mongo.password}" />
<!-- 映射转换器,扫描back-package目录下的文件(当前使用注解方式),根据注释,把它们作为mongodb的一个collection的映射 -->
<bean id="mongoConverter"
class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg name="mongoDbFactory" ref="factory" />
<constructor-arg name="mappingContext" ref="mongoMappingContext" />
<!-- 实体反序列化时使用属性而非字段 spring data mongodb 1.7不支持此属性 -->
<!-- <property name="useFieldAccessOnly" value="false" /> -->
</bean>
<!-- mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成 -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="factory" />
<constructor-arg name="mongoConverter" ref="mongoConverter" />
<property name="writeResultChecking" value="EXCEPTION" />
</bean>
<!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 -->
<mongo:repositories base-package="com.***.mongo.repository"
factory-class="com.***.repository.support.BaseRepositoryFactoryBean"
mongo-template-ref="mongoTemplate">
</mongo:repositories>
</beans>
2、相关的代码:
(1)基础类BaseRepository:
*/
@NoRepositoryBean
public interface BaseRepository<T,ID extends Serializable> extends MongoRepository<T, ID> {
}
(2)BaseRepository实现类:BaseRepositoryImpl
public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleMongoRepository<T, ID> implements BaseRepository<T,ID> {
private final MongoOperations mongoOperations;
public BaseRepositoryImpl(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
super(metadata, mongoOperations);
this.mongoOperations = mongoOperations;
}
}
(3)BaseRepositoryFactoryBean类:动态注入相关的配置信息
public class BaseRepositoryFactoryBean<R extends MongoRepository<T, I>, T, I extends Serializable> extends
MongoRepositoryFactoryBean<R, T, I> {
@Override
public RepositoryFactorySupport getFactoryInstance(
MongoOperations operations) {
return new EntityRepositoryFactory<T, Serializable>(operations);
}
private static class EntityRepositoryFactory<T, I extends Serializable> extends MongoRepositoryFactory {
private MongoOperations mongoOperations;
public EntityRepositoryFactory(MongoOperations mongoOperations) {
super(mongoOperations);
this.mongoOperations = mongoOperations;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected Object getTargetRepository(RepositoryMetadata metadata) {
MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType());
return new BaseRepositoryImpl(entityInformation, mongoOperations);
}
@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return BaseRepository.class;
}
}
}
3、使用方法:
@Autowired
private BaseRepository<**Entity, String> baseRepository;