参考原文: http://412887952-qq-com.iteye.com/blog/2293846 使用的是在spring中注入一个bean的方式来测试是否成功, 感觉略不实用, 只碰到过一次dubbo中需要配置文件注入, 其他都可以通过注解注入的方式实现

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=""
write-timeout="" 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=
mongo.connectionsPerHost=
mongo.threadsAllowedToBlockForConnectionMultiplier=
mongo.connectTimeout=
mongo.maxWaitTime=
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
mongo.socketTimeout=
mongo.slaveOk=true
mongo.writeNumber=
mongo.riteTimeout=
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

springboot-16-springboot中引入xml文件的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource

    1. package soundsystem; import org.springframework.beans.factory.annotation.Autowired; public class ...

  2. Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件

    1.Spring依赖注入的方式 通过set方法完成依赖注入 通过构造方法完成依赖注入 2.依赖注入的类型 基本数据类型和字符串 使用value属性 如果是指向另一个对象的引入 使用ref属性 User ...

  3. .net中创建xml文件的两种方法

    .net中创建xml文件的两种方法 方法1:根据xml结构一步一步构建xml文档,保存文件(动态方式) 方法2:直接加载xml结构,保存文件(固定方式) 方法1:动态创建xml文档 根据传递的值,构建 ...

  4. [Eclipse] eclipse中打开xml文件,使用ctrl+鼠标左键无法跳转至Java源文件【待解决】

    eclipse中打开xml文件,使用ctrl+鼠标左键无法跳转至Java源文件: 1. 设置eclipse ctrl + 左键打开源文件代码,如下图,设置都正常 2. 在网上找了很多种办法,均失败,在 ...

  5. 解决Eclipse中编辑xml文件的智能提示问题,最简单的是第二种方法。

    Eclipse for Android xml 文件代码自动提示功能,介绍Eclipse 编辑器中实现xml 文件代码自动智能提示功能,解决eclipse 代码提示失效.eclipse 不能自动提示. ...

  6. 修改Android中strings.xml文件, 动态改变数据

    有些朋友可能会动态的修改Android中strings.xml文件中的值,在这里给大家推荐一种简单的方法.strings.xml中节点是支持占位符的,如下所示: <string name=&qu ...

  7. 『vue踩坑日常』 在index.html中引入静态文件不生效

    Vue日常踩坑日常 -- 在index.html中引入静态文件不生效问题 本文针对的是Vue小白,不喜勿喷,谢谢 出现该问题的标志如下 控制台warning(Resource interpreted ...

  8. [转]解决Eclipse中编辑xml文件的智能提示问题

    转自:http://hi.baidu.com/cghroom/item/48fd2d0dc1fc23c675cd3c3e 摘要:  Eclipse for Android xml 文件代码自动提示功能 ...

  9. 【jsp】怎么在jsp文件中引入静态文件(.js .css)

    如果在jsp文件中引入静态文件比如(.js或.css等等),可以在使用 /项目名称/路径 的方式,但是这种方式如果在修改了项目了名称后就显得比较麻烦了.除了之外还许多方式,比如相对路径等等.一般情况下 ...

随机推荐

  1. D3 drag

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. springMVC 开涛 Controller接口控制器

    通过注解实现控制器类,所以不用看Controller接口了.把之前的笔记保存下. 笔记(图片):http://pan.baidu.com/s/1mgMNDna 第三章看不太懂,3.2 3.3.只了解到 ...

  3. Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)

    Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...

  4. 测试一下你的T-SQL基础知识-count

    下面count的返回值是多少? ) ); GO INSERT mytable ( myid, mychar ) VALUES ( , 'A' ), ( , 'B'), ( NULL, 'C' ), ( ...

  5. Alwayson--配置只读请求

    场景:有三台服务器SQLNode131,SQLNode132 ,SQLNode133: 1) 三台服务器加入域DCDemo.com 2) 三台服务器使用端口访问数据库 3) 三台服务器构建故障转移群集 ...

  6. asp.net—工厂模式

    一.什么是工厂模式 定义:定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类. 二.怎么使用工厂模式 首先模拟一个场景:有一个汽车工厂,  可以日本车.美国车.中国车... 这个场景怎么用工厂 ...

  7. Backbone学习笔记 - View篇

    Backbone是一种Web端的MVC框架,这里纪录学习Model,View和Collection的笔记. 1 View initialize构造函数 Backbone.View 与jQuery库紧密 ...

  8. jqury的ajax

    前端代码: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...

  9. SFML从入门到放弃(3) 视角和碰撞检测

    SFML从入门到放弃(3) 视角和碰撞检测 视角 window.draw();所画出的对象是在世界坐标的绝对位置. 视角可以选定在窗口中显示世界坐标下的的哪一个区域. sf::View init_vi ...

  10. Django 一些少用却很实用的orm查询方法

    一.使用Q对象进行限制条件之间 "或" 连接查询 from django.db.models import Q from django.contrib.auth.models im ...