简体   繁体   English

如何从属性文件填充使用spring配置的地图

[英]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.

相关问题 如何使用spring从属性文件中加载地图 - how to load map from properties file with spring Spring:将属性文件注入map - Spring: inject properties file into map 在应用程序上下文中从.properties文件访问地图属性(春季) - accessing map property from .properties file in application context (Spring) 从属性文件中读取Map并使用spring注释加载@Value - Reading a Map from properties file and load with spring annotation @Value 如何将地图存储在Spring项目使用的.properties文件中? - How to store map in .properties file that is used by Spring project? 如何从数据库而不是Spring应用程序中的属性文件加载属性 - How to load properties from database instead of properties file in spring application 如何使用属性文件中的值在Spring属性中进行算术运算? - How to do arithmetic in Spring properties, with values from a properties file? 如何在配置了 Java Annotations 并使用 log4j.properties 文件的 Spring Mvc 应用程序中配置 log4j - How to configure log4j in a Spring Mvc application configured with Java Annotations and using a log4j.properties file 如何在 Spring 中使用带有点属性的 @Configuration 属性前缀映射属性文件? - How map properties file using @Configuration properites prefix with dotted properties in Spring? 如何使用 Spring @Value 从 java 属性文件中填充 HashMap - How to fill HashMap from java property file with Spring @Value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM