返回顶部

收藏

spring mongodb的常规配置

更多

application-context-repository.xml(配置文件) <?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/data/mongohttp://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd ">

<!--向Spring容器注册AutowiredAnnotationBeanPostProcessor、 CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。 注册这4个BeanPostProcessor的作用,就是为了能够识别相应的注解--> <context:annotation-config/>

<!--Spring定时器注解开关--> <task:annotation-driven/>

<!--使用注解一般都会配置扫描包路径选项--> <context:component-scanbase-package="com.studyspring.mongodb"/> <!--读取mongodb.properties文件--> <context:property-placeholderlocation="classpath:mongodb.properties"/>

<!--配置mongo数据库--> <mongo:mongohost="${mongo.host}"port="${mongo.port}"> </mongo:mongo>

<!--通过工厂Bean创建mongo连接实例--> <mongo:db-factoryid="mongoDbFactory"dbname="${mongo.database}"username="${mongo.user}"password="${mongo.password}"/>

<!--映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射--> <mongo:mapping-converterbase-package="com.studyspring.mongodb.converter"id="customCVT"> <mongo:custom-convertersbase-package="com.studyspring.mongodb.converter"/> </mongo:mapping-converter>

<!--mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成--> <beanid="mongoTemplate"class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-argname="mongoDbFactory"ref="mongoDbFactory"/> <constructor-argref="customCVT"/> <propertyname="writeConcern"value="SAFE"/> </bean>

<!-- mongodbbean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 --> <mongo:repositoriesbase-package="com.studyspring.mongodb.repositories"/>

<!--日志事件监听--> <beanclass="org.springframework.data.mongodb.core.mapping.event.LoggingEventListener"/> </beans>

标签:java

收藏

0人收藏

支持

0

反对

0

发表评论