springboot-16-springboot中引入xml文件

参考原文: 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">
&lt;context:component-scan <span style="color: rgba(0, 0, 255, 1)">base</span>-package=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">com.mongo</span><span style="color: rgba(128, 0, 0, 1)">"</span> /&gt;
&lt;!-- 获取配置资源 --&gt;
&lt;!-- &lt;context:property-placeholder location=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">classpath:mongodb-context-config.properties</span><span style="color: rgba(128, 0, 0, 1)">"</span> /&gt; --&gt;
&lt;context:property-placeholder location=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">classpath:mongodb-context-config.properties</span><span style="color: rgba(128, 0, 0, 1)">"</span> ignore-unresolvable=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">true</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt; 

&lt;mongo:mongo id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mongo</span><span style="color: rgba(128, 0, 0, 1)">"</span> host=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${mongo.host}</span><span style="color: rgba(128, 0, 0, 1)">"</span> port=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${mongo.port}</span><span style="color: rgba(128, 0, 0, 1)">"</span> &gt;
    &lt;<span style="color: rgba(0, 0, 0, 1)">mongo:options 
        connections</span>-per-host=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${mongo.connectionsPerHost}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> 
        threads</span>-allowed-to-block-<span style="color: rgba(0, 0, 255, 1)">for</span>-connection-multiplier=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${mongo.threadsAllowedToBlockForConnectionMultiplier}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> 
        connect</span>-timeout=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${mongo.connectTimeout}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> 
        max</span>-wait-time=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${mongo.maxWaitTime}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> 
        auto</span>-connect-retry=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${mongo.autoConnectRetry}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> 
        socket</span>-keep-alive=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${mongo.socketKeepAlive}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> 
        socket</span>-timeout=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${mongo.socketTimeout}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> 
        slave</span>-ok=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${mongo.slaveOk}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> 
        write</span>-number=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> 
        write</span>-timeout=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">0</span><span style="color: rgba(128, 0, 0, 1)">"</span> write-fsync=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">true</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt; 
&lt;/mongo:mongo&gt;
&lt;!-- 设置使用的数据库 名--&gt;
&lt;mongo:db-factory dbname=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">"</span> mongo-<span style="color: rgba(0, 0, 255, 1)">ref</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mongo</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt;
&lt;!-- mongodb的模板 --&gt;
&lt;bean id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mongoTemplate</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 0, 255, 1)">class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">org.springframework.data.mongodb.core.MongoTemplate</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
   &lt;constructor-arg name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mongoDbFactory</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 0, 255, 1)">ref</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mongoDbFactory</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt;
&lt;/bean&gt;

</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