簡體   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