简体   繁体   English

如何使用Spring配置将文本文件内容设置为bean属性

[英]How to set text file content to bean property using Spring configuration

I have a text file which contains data. 我有一个包含数据的文本文件。 I want to set this data to a String instance variable. 我想将此数据设置为String实例变量。

I know I can do it using Java I/O, read the file and set it's content to String variable. 我知道我可以使用Java I / O进行操作,读取文件并将其内容设置为String变量。

But is there a simplistic way to initialize the String using Spring configuration file? 但是,使用Spring配置文件初始化String的方法是否简单?

Try using properties file with this xml tags: 尝试使用带有以下xml标签的属性文件:

<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:YourFile.properties" />
    </bean>

And use the values as ${your_key} 并将这些值用作$ {your_key}

Yes, you can do that using Resource and then use getFile() on resource. 是的,您可以使用Resource ,然后在resource上使用getFile()。 Check this spring documentation. 检查春季文档。 And better example here . 而更好的例子在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Spring XML元数据配置中为bean设置ServletContext属性 - How to set ServletContext property for a bean in Spring XML metadata configuration 使用来自其他bean属性的xml配置设置Spring bean属性 - Spring bean property set using xml configuration from other bean property 在 Spring Boot 中使用 @Bean 配置设置类属性的默认值 - Set default value of class property with @Bean configuration in Spring Boot 如何使用Spring XML配置来设置包含特定类型的所有bean的列表的bean属性? - How can I use Spring XML configuration to set a bean property with list of all beans of a certain type? 如何将Class值设置为spring bean属性? - How to set Class value to spring bean property? 在使用rich:fileUpload上传文件之前,如何设置Bean属性? - How to set a bean property before uploading a file using rich:fileUpload? 使用 Spring 配置文件设置系统属性 - Set System Property With Spring Configuration File 如何使用.properties文件中的值初始化spring bean中的集合 - How to initialize a set in a spring bean using value from .properties file 如何将一个 spring bean 的父属性设置为另一个 bean 的属性? - How to set the parent attribute of one spring bean to a property of another bean? 如何使用Spring贡献bean的list属性? - How to contribute to a list property of a bean using Spring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM