Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I load nested key value pairs from a properties file into a Java object using Spring?
    primarykey
    data
    text
    <p>I understand how to use Spring with the PropertyPlaceholderConfigurer to load a .properties file when we know what properties to expect, and use @Value to store those values into variables or some object.</p> <p>However, how do I have Spring load up a properties file with nested key,value pairs when the keys can vary?</p> <p>For example, lets say I had the following car.properties file:</p> <pre><code>Chevy=Corvette:String,1234567890:long,sportsCar:String Honda=Odyssey:String,2345678910:long,minivan:String Ford=F350:String,4567891011:long,truck:String </code></pre> <p>where each line of the properties file has a key which is the make, followed by three nested key,value pairs i.e., one for the model, one for the VIN, and one for the vehicle type i.e.,</p> <pre><code>&lt;make&gt;=&lt;model&gt;:&lt;dataType&gt;,&lt;vin&gt;:&lt;dataType&gt;,&lt;vehicleType&gt;:&lt;dataType&gt; </code></pre> <p>I'm using this structure since future vehicles will be added later, and I don't want to change my underlying Java code. And let's say I want to use these vehicle properties to generate some random data about vehicles for testing.</p> <p>How would I use Spring to load each line of the properties file as a collection of vehicle values to be stored in an arraylist? I'm figuring I'd have a 2D arraylist where each of these vehicles would be an arraylist inside the "all vehicles" arraylist. Then I would randomly select one of the vehicle arraylists to generate dummy vehicle data.</p> <p>Anyway, I think I'm on the right track, but just can't seem to figure out how I would load my nested key,value pairs using Spring. Any suggestions?</p> <p>UPDATED context.xml that works for me:</p> <p>Btw, here is the context.xml I'm using:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"&gt; &lt;!-- creates a java.util.Properties instance with values loaded from the supplied location --&gt; &lt;util:properties id="carProperties" location="classpath:/car.properties"/&gt; &lt;bean class="com.data.rcgen.generator.CarLoader"&gt; &lt;property name="sourceProperties" ref="carProperties" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload