繁体   English   中英

使用Spring初始化Properties对象

[英]Initializing a Properties object using Spring

我有一个扩展属性的类,该属性用于存储一些专用键名:

public class StorageConfiguration extends Properties {
    private final String PROPERTY_NAME_1 = "property.key";

    public String getProperty1() {
        return this.getProperty(PROPERTY_NAME_1);
    }

    public void setProperty1(String property1) {
        this.setProperty(PROPERTY_NAME_1, property1);
    }
}

使用这些属性的类:

public class Storage {
    StorageConfiguration storageConfiguration;

    @Autowired
    public void setStorageConfiguration(StorageConfiguration storageConfiguration) {
        this.storageConfiguration = storageConfiguration;
    }

    public void init() {
        // Initialize properties in this class using StorageConfiguration.
    }
}

我已经设置了Spring来初始化Storage和StorageConfiguration,如下所示:

<bean id="storage" class="com.k4rthik.labs.Storage" init-method="init">
    <property name="storageConfiguration" ref="storageConfiguration" />
</bean>
<bean id="storageConfiguration"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="storageConfiguration">
        <props>
            <prop key="property.key">property_value</prop>
        </props>
    </property>
</bean>

我期望发生的事情是Spring将通过将属性“ property.key”设置为“ property_value”来初始化StorageConfiguration对象。

但是我得到以下异常

org.springframework.beans.factory.BeanCreationException:在类路径资源[applicationContext.xml]中创建名称为“ storage”的bean时出错:设置bean属性“ authorizationConfig”时无法解析对bean“ storageConfiguration”的引用; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建在类路径资源[applicationContext.xml]中定义的名称为'authorizationConfig'的bean时出错:设置属性值时出错; 嵌套的异常是org.springframework.beans.NotWritablePropertyException:Bean类[org.springframework.beans.factory.config.PropertiesFactoryBean]的无效属性'storageConfiguration':Bean属性'storageConfiguration'无法写或具有无效的setter方法。 setter的参数类型是否与getter的返回类型匹配?

如您所见,我在Storage类中有一个用于storageConfiguration的自动连线设置器,因此我真的看不到这里有什么问题。

PropertiesFactoryBean创建一个类型为Properties的bean。

要创建StorageConfiguration,您可以创建一个Copy构造函数

public class StorageConfiguration
{
    public StorageConfiguration(Properties defaults) {
        super(defaults);
    }
}

然后这应该工作:

<bean id="storageConfiguration" class="..StorageConfiguration">
  <constructor-arg>

   <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="property.key">property_value</prop>
        </props>
    </property>
  <bean>
  </constructor-arg>
</bean>

甚至:

<bean id="storageConfiguration" class="..StorageConfiguration">
  <constructor-arg>   
        <props>
            <prop key="property.key">property_value</prop>
        </props>
  </constructor-arg>
</bean>

它应该是

<bean id="storage" class="com.k4rthik.labs.Storage" init-method="init">
    <property name="storageConfiguration">
         <props>
            <prop key="property.key">property_value</prop>
        </props>
    </property>
</bean>

配置

<bean id="storageConfiguration"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="storageConfiguration">
        <props>
            <prop key="property.key">property_value</prop>
        </props>
    </property>
</bean>

表示类型为org.springframework.beans.factory.config.PropertiesFactoryBean StorageConfiguration具有一个名为storageConfiguration的属性,该属性与您的代码不同

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM