当我们知道需要什么属性时,我了解如何使用 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
其中属性文件的每一行都有一个键,即品牌,后跟三个嵌套的键值对,即,一个用于模型,一个用于 VIN,一个用于车辆类型,即,
<make>=<model>:<dataType>,<vin>:<dataType>,<vehicleType>:<dataType>
我使用这种结构是因为以后会添加 future 的车辆,而且我不想更改我的底层 Java 代码。假设我想使用这些车辆属性来生成一些有关车辆的随机数据以进行测试。
如何使用 Spring 将属性文件的每一行加载为要存储在数组列表中的车辆值集合?我想我会有一个 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 不可能为你做这件事。您需要自己实现解析。不过,spring可以为你提供一些方便的实用类:
- 您可以通过
util:properties
加载属性文件配置元素 - 您可以使用
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;
}
}
<小时/>
更新如何从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());
}
}
}
关于java - 如何使用 Spring 将嵌套键值对从属性文件加载到 Java 对象中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19185751/