首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot-3-springboot中引入xml文件

springboot-3-springboot中引入xml文件

作者头像
用户5640963
发布2019-07-26 12:24:29
1.3K0
发布2019-07-26 12:24:29
举报
文章被收录于专栏:卯金刀GG卯金刀GG

1, mongdb的配置文件:

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    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-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
    http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <context:component-scan base-package="com.mongo" />
    <!-- 获取配置资源 -->
    <!-- <context:property-placeholder location="classpath:mongodb-context-config.properties" /> -->
    <context:property-placeholder location="classpath:mongodb-context-config.properties" ignore-unresolvable="true"/> 
    
    <mongo:mongo id="mongo" host="${mongo.host}" port="${mongo.port}" >
        <mongo:options 
            connections-per-host="${mongo.connectionsPerHost}" 
            threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" 
            connect-timeout="${mongo.connectTimeout}" 
            max-wait-time="${mongo.maxWaitTime}" 
            auto-connect-retry="${mongo.autoConnectRetry}" 
            socket-keep-alive="${mongo.socketKeepAlive}" 
            socket-timeout="${mongo.socketTimeout}" 
            slave-ok="${mongo.slaveOk}" 
            write-number="1" 
            write-timeout="0" write-fsync="true"/> 
    </mongo:mongo>
    <!-- 设置使用的数据库 名-->
    <mongo:db-factory dbname="test" mongo-ref="mongo"/>
    <!-- mongodb的模板 -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
       <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
    </bean>
</beans>

资源文件:

mongo.host=www.wenbronk.com
mongo.port=27017
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
mongo.connectTimeout=1000
mongo.maxWaitTime=1500
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
mongo.socketTimeout=1500
mongo.slaveOk=true
mongo.writeNumber=1
mongo.riteTimeout=0
mongo.writeFsync=true

2, 将配置文件引入springboot

XMLSource.java 确保可以被入口程序扫描到

package com.iwhere;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

/**
 * 将外部资源文件引入springboot中
 * 在入口类的可扫描环境下
 * 
 * @author wenbronk
 * @time 2017年4月6日  下午2:21:30  2017
 */
@Configuration
@ImportResource(locations={"classpath:mongodb/mongodb-context.xml"})
public class XMLSource {

    
}

然后在启动后, 就可以通过MongoTemplate来进行注入了

原文地址: http://412887952-qq-com.iteye.com/blog/2293846

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1, mongdb的配置文件:
  • 2, 将配置文件引入springboot
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档