简体   繁体   English

使用Spring初始化Properties对象

[英]Initializing a Properties object using Spring

I have a class that extends Properties used to store some specialized key names: 我有一个扩展属性的类,该属性用于存储一些专用键名:

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);
    }
}

And a class that uses these properties: 使用这些属性的类:

public class Storage {
    StorageConfiguration storageConfiguration;

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

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

I have my Spring set up to initialize Storage and StorageConfiguration like so: 我已经设置了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>

What I expected would happen was Spring would initialize the StorageConfiguration object by setting the property "property.key" to "property_value". 我期望发生的事情是Spring将通过将属性“ property.key”设置为“ property_value”来初始化StorageConfiguration对象。

However I get the following exception 但是我得到以下异常

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'storage' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'storageConfiguration' while setting bean property 'authorizationConfig'; org.springframework.beans.factory.BeanCreationException:在类路径资源[applicationContext.xml]中创建名称为“ storage”的bean时出错:设置bean属性“ authorizationConfig”时无法解析对bean“ storageConfiguration”的引用; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authorizationConfig' defined in class path resource [applicationContext.xml]: Error setting property values; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建在类路径资源[applicationContext.xml]中定义的名称为'authorizationConfig'的bean时出错:设置属性值时出错; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'storageConfiguration' of bean class [org.springframework.beans.factory.config.PropertiesFactoryBean]: Bean property 'storageConfiguration' is not writable or has an invalid setter method. 嵌套的异常是org.springframework.beans.NotWritablePropertyException:Bean类[org.springframework.beans.factory.config.PropertiesFactoryBean]的无效属性'storageConfiguration':Bean属性'storageConfiguration'无法写或具有无效的setter方法。 Does the parameter type of the setter match the return type of the getter? setter的参数类型是否与getter的返回类型匹配?

As you can see, I have an autowired setter for storageConfiguration in the Storage class, so I don't really see what's wrong here. 如您所见,我在Storage类中有一个用于storageConfiguration的自动连线设置器,因此我真的看不到这里有什么问题。

PropertiesFactoryBean creates a bean of type Properties. PropertiesFactoryBean创建一个类型为Properties的bean。

To create a StorageConfiguration, you could create a Copy constructor 要创建StorageConfiguration,您可以创建一个Copy构造函数

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

Then this should work: 然后这应该工作:

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

Or even: 甚至:

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

it should be 它应该是

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

the config 配置

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

means that StorageConfiguration of type org.springframework.beans.factory.config.PropertiesFactoryBean has a property called storageConfiguration , which doesn't look like as per your code 表示类型为org.springframework.beans.factory.config.PropertiesFactoryBean StorageConfiguration具有一个名为storageConfiguration的属性,该属性与您的代码不同

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

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