如何传递 Map<String, String>使用 application.properties [英] How to pass a Map&lt;String, String&gt; with application.properties

查看:30
本文介绍了如何传递 Map<String, String>使用 application.properties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在需要配置的网络服务中实现了一些授权.

I have implemented some authorization in a webservice that needs be configured.

目前用户/密码组合被硬编码到 bean 配置中.我想使用用户和密码将地图配置到 application.properties 中,以便配置可以是外部的.

Currently the user/password combo is hardcoded into the bean configuration. I would like to configure the map with users and passwords into the application.properties so the configuration can be external.

关于如何做到这一点的任何线索?

Any clue on how this can be done?

<bean id="BasicAuthorizationInterceptor" class="com.test.BasicAuthAuthorizationInterceptor">
    <property name="users">
        <map>
            <entry key="test1" value="test1"/>
            <entry key="test2" value="test2"/>
        </map>
    </property>
</bean>

推荐答案

A java.util.Properties 对象已经是一个 Map,实际上是一个 HashTable,它反过来实现了 地图.

A java.util.Properties object is already a Map, actually a HashTable which in turn implements Map.

因此,当您创建一个属性文件(让其命名为 users.properties)时,您应该能够使用 PropertiesFactoryBean<util 加载它:properties/> 并将其注入到您的类中.

So when you create a properties file (lets name it users.properties) you should be able to load it using a PropertiesFactoryBean or <util:properties /> and inject it into your class.

test1=test1
test2=test2

然后做类似的事情

<util:properties location="classpath:users.properties" id="users" />

<bean id="BasicAuthorizationInterceptor" class="com.test.BasicAuthAuthorizationInterceptor">
    <property name="users" ref="users" />
</bean>

虽然如果你有一个 Map 作为用户属性的类型,它可能会失败......我不会把它们放在 application.properties代码>文件.但那可能只是我..

Although if you have a Map<String, String> as a type of the users property it might fail... I wouldn't put them in the application.properties file. But that might just be me..

这篇关于如何传递 Map<String, String>使用 application.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆