spring mongodb的常规配置
发布时间:2020-12-15 00:18:08 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 application-context-repository.xml(配置文件) ?xml version="1.0" encoding="UTF-8"? beans xmlns="http://www.springframework.org/schema/beans
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考
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> 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |