[英]How to fill map, configured with spring, from properties file
I have built a small app that reads an Excel file and creates all the necessary SQL statements for me. 我建立了一个小型应用程序,可以读取Excel文件并为我创建所有必要的SQL语句。 Excel file is manipulated by customers from time to time.
客户不时操纵Excel文件。
The first line of the Excel file includes a title I need to translate into integers when reading the lines. Excel文件的第一行包含一个标题,我在阅读这些行时需要将其转换为整数。 For example, the title "english" needs to be translated into "30" before I generate the SQL statements.
例如,在生成SQL语句之前,标题“英语”需要翻译成“ 30”。 (Just some internal definitions).
(只是一些内部定义)。 (You could compare it with DNS - Human readable domain name into IP-Address mapping) Currently I do the mapping manually, but I want to get rid of this step with a tiny Spring configuration.
(您可以将其与DNS相比-将人类可读的域名转换为IP地址映射)目前,我手动进行映射,但是我想通过一个很小的Spring配置来摆脱这一步骤。 Because the headlines are not fixed, it is necessary to read the headline information from a properties file.
由于标题不是固定的,因此有必要从属性文件中读取标题信息。
I found something like this: 我发现了这样的事情:
<bean class="java.util.HashMap" id="exampleMapping">
<constructor-arg index="0">
<map>
<entry key="theKey" value="theValue"/>
<entry key="otherKey" value="otherValue"/>
</map>
</constructor-arg>
</bean>
Which seems to work quite well. 这似乎工作得很好。 But the Spring configuration is compiled into a jar.
但是Spring配置被编译到jar中。 So my first choice (if possible) would be to externalize the key value pairs into a properties file.
因此,我的第一选择(如果可能的话)是将键值对外部化到属性文件中。
You're in luck, because the Properties
class implements Map
! 真幸运,因为
Properties
类实现Map
! Just define the properties bean like so: 像这样定义属性bean:
<util:properties id="myProps" location="classpath:myProps.properties" />
(Don't forget to import the Spring util
namespace) (不要忘记导入Spring
util
名称空间)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.