Spring:如何将属性文件中的值传递给构造函数

2024-02-06

我有一个MongoService类为

public class MongoService {

    private final Mongo mongo;
    private final String database;
    private static final Logger LOGGER = LoggerFactory.getLogger(MongoService.class);

    public MongoService(@Nonnull final String host, final int port, @Nonnull final String db) throws UnknownHostException {
        mongo = new Mongo(host, port);
        database = db;
    }

    public void putDocument(@Nonnull final DBObject document) {
        LOGGER.info("inserting document - " + document.toString());
        mongo.getDB(database).getCollection(getCollectionName(document)).insert(document, WriteConcern.SAFE);
    }

    public void putDocuments(@Nonnull final List<DBObject> documents) {
        for (final DBObject document : documents) {
            putDocument(document);
        }
    }

}

我想注入的值host, port, db into 构造函数来自外部属性文件
/storage/local.properties

### === MongoDB interaction === ###
host=127.0.0.1
port=27017
database=contract  

我的 Spring 连接文件如下所示
接线文件

<?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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">

    <util:properties id="mongoProperties" location="file:///storage//local.properties" />

    <bean id="mongoService" class="com.business.persist.MongoService">
        // TODO
    </bean>
</beans>

Question

我怎样才能传递的值host, port, db from local.properties文件并将其传递给以下构造函数?

public MongoService(@Nonnull final String host, final int port, @Nonnull final String db) throws UnknownHostException {
        mongo = new Mongo(host, port);
        database = db;
    }

您不想使用 util:properties 标记导入属性文件,而是使用 context:property-placeholder 导入它。 util 版本只是将文件作为 Properties 对象导入,而不是向您的配置公开属性值。所以你的设置会是这样的:

<context:property-placeholder location="file:///storage//local.properties"/>

然后,当您连接 MongoService 时,您可以在构造函数配置中使用属性名称,例如

<bean id="mongoService" class="com.business.persist.MongoService">
    <constructor-arg value="${host}"/>
    <constructor-arg value="${port}"/>
    <constructor-arg value="${database}"/>
</bean>

See 春季文档 http://static.springsource.org/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-placeholderconfigurer更多细节。 另外,我会考虑为每个属性提供更具描述性的名称,以避免与应用程序中可能定义的其他属性发生冲突。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring:如何将属性文件中的值传递给构造函数 的相关文章

随机推荐

  • 是否可以在 django 中指定外部 URL 名称

    我刚刚开始在 Django 中命名我的 URL 模式 因此如果我想更改 URL 模式 我只需在一处更改它即可 例如 url r HomeListView as view name home 并在我的模板中引用它 如下所示 url home
  • 如何跳出父函数?

    如果我想打破一个函数 我可以调用return 如果我在子函数中并且想跳出调用子函数的父函数怎么办 有没有办法做到这一点 一个最小的例子 def parent print Parent does some work print Parent
  • Python午夜时间对比

    我必须节省时间AM PM format 但我在决定如何输入午夜时间时遇到困难 假设时间是晚上 9 点到第二天早上 6 点 我必须把它分成日常的基础 像这样 t1 datetime datetime strptime 09 00PM I M
  • 检查两个集合中是否存在任何元素

    我想知道 Linq 是否有一种方法来检查两个集合是否至少有一个共同元素 我期待这样的事情 var listA new List
  • 将包含“id,attribute,value”列的表转换为“id,attribute1,attribute2,...”

    我在数据库中有一个表 如下所示 CustomerId Attribute Value 30 Name John 30 Surname Smith 30 Phone 555123456 为了充分利用这些数据 我需要让它看起来像某样东西 像这样
  • Spritebatch.Begin() 变换矩阵

    我一直想知道 spriteBatch 中的变换矩阵是如何实现的 我创建了一个2D相机 变换矩阵如下 if needUpdate transformMatrix Matrix CreateTranslation Position X Posi
  • 能否强制 AVFoundation 播放本地 .ts 文件?

    显然 AVFoundation 和 Quicktime X 可以解复用并正确播放编码 ts容器 因为 tsHTTPS 实时流媒体底层的容器 缺乏建立本地网络服务来服务 m3u8以及相关的 ts文件 我真的希望能够 说服AVURLAsset
  • 如何使用 CoreNLP 的 RegexNER 检测具有超过 1 个单词的命名实体?

    我在 CoreNLP 中使用 RegexNER 注释器 我的一些命名实体由多个单词组成 摘自我的映射文件 RAF 抑制剂 DRUG CLASS 吉尔伯特综合症 疾病 第一个被检测到 但每个单词都获得注释 DRUG CLASS 并且似乎没有办
  • 有什么办法可以将表格中的某些列居中吗?

    我广泛使用 css 格式并为表定义类 其中包含 thead tbody tfoot 的子类以及 tr th td 的另一级子类 在某些情况下 我想让表的整列具有特定样式的类 但不知道该怎么做 有没有什么方法 使用 HTML5 CSS3 其他
  • Linq .Contains 包含大集合会导致 TDS 错误

    我有点过于简单化了 因为我正在寻找通用的答案 假设我有一个这样的表设置 Parent recno int unique pk date datetime stuff varchar 50 Child parentrecno int fk P
  • Android Studio 调试错误“连接被拒绝”

    我知道以前有人问过这个问题 但我尝试了所有建议的解决方案 但还没有解决我的问题 我无法使用 Mac Yosemite 调试任何 Android studio 1 5 预览版 2 应用程序 我得到的错误是 Unable to open deb
  • 建议像 google 一样使用 postgresql trigrams 和全文搜索

    我想做一个文本搜索 我在用着PostgreSQL http www postgresql org 因为神奇的Postgis http postgis refractions net 我正在考虑使用FTS http www postgresq
  • Aarch64 上 C++11 原子的部分重新排序

    我正在看gcc 的 rmw 原子的编译器输出 https goo gl ZWLeCJ并注意到一些奇怪的事情 在 Aarch64 上 诸如 fetch add 之类的 rmw 操作可以通过宽松的负载进行部分重新排序 在 Aarch64 上 可
  • 获取磁盘上文件的所有者

    下面是我当前的宏 它运行得很好 但我想在 Cells r 10 中添加文件的所有者 这样的事情怎么可能完成呢 我找不到像 File Owner 之类的命令 Sub DoFolder Folder If Ans vbNo Then GoTo
  • Elasticsearch - 匹配字符串或空值

    我必须检查某个字段是否与特定文本匹配或为空 可以这样做吗 谢谢 您可以通过使用来实现这一点missing http www elasticsearch org guide en elasticsearch reference current
  • TypeScript 接口数组类型错误 TS2411

    我很难理解这一点 支持的索引类型有两种 字符串和数字 可以支持两种类型的索引 但限制是从数字索引返回的类型必须是从字符串索引返回的类型的子类型 虽然索引签名是描述数组和 字典 模式的强大方法 但它们还强制所有属性与其返回类型相匹配 在此示例
  • Android WebRTC 中的本地视频渲染器

    我正在使用这个库 https bintray com google webrtc google webrtc https bintray com google webrtc google webrtc 我想要实现的目标 至少在我的项目开始时
  • 共享库如何知道它所在的位置?

    我正在为 Linux 机器开发一个共享库 它是通过 rpath 相对于主可执行文件动态加载的 现在 库本身尝试相对于其位置动态加载其他库 但没有 rpath 我使用 scandir 来搜索某个文件夹中的共享库 我还不知道它们的名称 仅当工作
  • 正则表达式匹配不包含任何超过 10 个字符的单词的字符串?

    S 10 我有一个正则表达式 它将匹配任何包含 10 个字符的单词的字符串 然而我需要它的反面 一个正则表达式 仅匹配不包含 gt 10 个字符的单词的字符串 使用否定断言 S 10 S 10 匹配 10 的序列 S 它必须是更长的子序列
  • Spring:如何将属性文件中的值传递给构造函数

    我有一个MongoService类为 public class MongoService private final Mongo mongo private final String database private static fina