简体   繁体   中英

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:

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

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'; 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; 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. Does the parameter type of the setter match the return type of the 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.

PropertiesFactoryBean creates a bean of type Properties.

To create a StorageConfiguration, you could create a Copy constructor

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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