如何传递 Map<String, String>使用 application.properties

编程入门 行业动态 更新时间:2024-10-11 11:18:43
本文介绍了如何传递 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

本文发布于:2023-11-25 10:27:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:String   lt   Map   properties   application

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!