java - 如何使用 Spring 将嵌套键值对从属性文件加载到 Java 对象中?

标签 java spring properties nested key-value

当我们知道需要什么属性时,我了解如何使用 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 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/

相关文章:

java - 未找到 ResourceBundle 文件,属性文件不在 jar 中

iphone - objective-c : i want the automatic memory management of properties, 但无法从其他类访问

Java 集合 - 如何在集合中添加多个值?

spring - 属性文件中的 persistence.xml 属性值

java - 如何在 Spring AOP 上获取 Web session

spring 在集成测试中启动并行事务

Maven 动态更改属性(运行时)

java - 为什么我的 Java/Libgdx 9 秒倒计时器不工作?

java - 将 openssh 公钥转换为 ssh2 (RFC 4716) 格式

Java:使用CTR模式的AES加密;无法解密