spring mongodb的常规配置
2018-07-20 来源:open-open
application-context-repository.xml(配置文件)
<?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: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/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://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-scan base-package="com.studyspring.mongodb" />
<!-- 读取mongodb.properties文件 -->
<context:property-placeholder location="classpath:mongodb.properties"/>
<!-- 配置mongo数据库 -->
<mongo:mongo host="${mongo.host}" port="${mongo.port}">
</mongo:mongo>
<!-- 通过工厂Bean创建mongo连接实例-->
<mongo:db-factory id="mongoDbFactory" dbname="${mongo.database}" username="${mongo.user}" password="${mongo.password}"/>
<!-- 映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射 -->
<mongo:mapping-converter base-package="com.studyspring.mongodb.converter" id="customCVT">
<mongo:custom-converters base-package="com.studyspring.mongodb.converter" />
</mongo:mapping-converter>
<!-- mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成 -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<constructor-arg ref="customCVT"/>
<property name="writeConcern" value="SAFE" />
</bean>
<!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 -->
<mongo:repositories base-package="com.studyspring.mongodb.repositories"/>
<!-- 日志事件监听 -->
<bean class="org.springframework.data.mongodb.core.mapping.event.LoggingEventListener"/>
</beans>
<?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: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/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://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-scan base-package="com.studyspring.mongodb" />
<!-- 读取mongodb.properties文件 -->
<context:property-placeholder location="classpath:mongodb.properties"/>
<!-- 配置mongo数据库 -->
<mongo:mongo host="${mongo.host}" port="${mongo.port}">
</mongo:mongo>
<!-- 通过工厂Bean创建mongo连接实例-->
<mongo:db-factory id="mongoDbFactory" dbname="${mongo.database}" username="${mongo.user}" password="${mongo.password}"/>
<!-- 映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射 -->
<mongo:mapping-converter base-package="com.studyspring.mongodb.converter" id="customCVT">
<mongo:custom-converters base-package="com.studyspring.mongodb.converter" />
</mongo:mapping-converter>
<!-- mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成 -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<constructor-arg ref="customCVT"/>
<property name="writeConcern" value="SAFE" />
</bean>
<!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 -->
<mongo:repositories base-package="com.studyspring.mongodb.repositories"/>
<!-- 日志事件监听 -->
<bean class="org.springframework.data.mongodb.core.mapping.event.LoggingEventListener"/>
</beans>
标签: 数据库
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐