Reputation: 1053
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.
Upvotes: 1
Views: 9481
Reputation: 17846
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>
Upvotes: 4
Reputation: 17622
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
Upvotes: 3