![](/img/trans.png)
[英]How to load nested key value pairs from a properties file in SpringBoot
[英]How do I load nested key value pairs from a properties file into a Java object using Spring?
我了解在知道所需的属性时如何将Spring与PropertyPlaceholderConfigurer一起使用以加载.properties文件,并使用@Value将这些值存储到变量或某个对象中。
但是,当键可以变化时,我如何让Spring用嵌套的键,值对加载属性文件?
例如,假设我有以下car.properties文件:
Chevy=Corvette:String,1234567890:long,sportsCar:String
Honda=Odyssey:String,2345678910:long,minivan:String
Ford=F350:String,4567891011:long,truck:String
其中,属性文件的每一行都有一个键,即make,然后是三个嵌套键值对,即一对用于模型,一对用于VIN,以及一对用于车辆类型,即
<make>=<model>:<dataType>,<vin>:<dataType>,<vehicleType>:<dataType>
我之所以使用这种结构,是因为以后会增加以后的载具,并且我不想更改底层的Java代码。 假设我想使用这些车辆属性来生成一些有关车辆的随机数据以进行测试。
我将如何使用Spring加载属性文件的每一行作为要存储在arraylist中的车辆值的集合? 我在想我会有一个2D数组列表,其中每个载具都将是“所有载具”数组列表中的一个数组列表。 然后,我将随机选择车辆阵列列表之一以生成虚拟车辆数据。
无论如何,我认为我走在正确的轨道上,但是似乎无法弄清楚如何使用Spring加载嵌套的键值对。 有什么建议么?
更新的context.xml对我有用:
顺便说一句,这是我正在使用的context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<util:properties id="carProperties" location="classpath:/car.properties"/>
<bean class="com.data.rcgen.generator.CarLoader">
<property name="sourceProperties" ref="carProperties" />
</bean>
</beans>
春天是不可能为您做到这一点的。 您将需要自己实现解析。 但是,spring可以为您提供一些便利实用程序类:
util:properties
config元素加载属性文件 BeanWrapperImpl
帮助您设置自定义bean的属性。 示例 (可能包含错别字):
<util:properties id="carProperties" location="classpath:car.properties"/>
<bean class="my.package.CarLoader">
<property name="sourceProperties" ref="carProperties" />
</bean>
public class Car {
private String name;
private String category;
// ... Getters and setters
}
public class CarLoader {
private Properties sourceProperties;
public List<Car> getCars() {
List<Car> cars = new ArrayList<Car>();
for (Object key : sourceProperties.keySet()) {
// Do the parsing - naive approach
String[] values = sourceProperties.getProperty((String) key).split(",");
// Create bean wrapper and set the parsed properties... will handle data convesions with
// default property editors or can use custom ConversionService via BeanWrapper#setConversionService
BeanWrapper wrappedCar = PropertyAccessorFactory.forBeanPropertyAccess(new Car());
wrappedCar.setPropertyValue("name", values[0].split(":")[0]); // Getting rid of the `:type`
wrappedCar.setPropertyValue("category", values[2].split(":")[0]); // Getting rid of the `:type`
// Phase 3 - prosper
cars.add((Car) wrappedCar.getWrappedInstance());
}
return cars;
}
public void setSourceProperties(Properties properties) {
this.sourceProperties = properties;
}
}
UPDATE基本示例如何从main
方法引导应用程序上下文:
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
CarLoader carLoader = context.getBean(CarLoader.class);
for (Car car : carLoader.getCars()) {
System.out.println("CAR - " + car.getName());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.