简体   繁体   中英

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.

I know I can do it using Java I/O, read the file and set it's content to String variable.

But is there a simplistic way to initialize the String using Spring configuration file?

Try using properties file with this xml tags:

<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}

Yes, you can do that using Resource and then use getFile() on resource. Check this spring documentation. And better example here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM