如何使用Spring将属性文件中的嵌套键值对加载到Java对象中?
我了解如何使用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,一个用于车辆类型,即:
=:,:,:
我正在使用这种结构,因为将来会添加未来的车辆,我不想更改我的底层Java代码。 并且假设我想使用这些车辆属性生成一些关于车辆的随机数据以进行测试。
我如何使用Spring将属性文件的每一行加载为要存储在arraylist中的车辆值的集合? 我想我会有一个2D arraylist,其中每辆车都是“所有车辆”arraylist中的arraylist。 然后我会随机选择一个车辆arraylists来生成虚拟车辆数据。
无论如何,我认为我在正确的轨道上,但似乎无法弄清楚如何使用Spring加载我的嵌套键,值对。 有什么建议么?
UPDATED context.xml对我有用:
顺便说一句,这是我正在使用的context.xml:
spring不会为你做这个。 您需要自己实现解析。 但是,spring可以为您提供一些方便的实用程序类:
- 您可以通过
util:properties
config元素加载属性文件 - 您可以使用
BeanWrapperImpl
来帮助您在自定义bean上设置属性。
示例 (可能包含拼写错误):
public class Car { private String name; private String category; // ... Getters and setters }
public class CarLoader { private Properties sourceProperties; public List getCars() { List cars = new ArrayList (); 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()); } } }