我已经成功地为除 java.util.Properties 实例之外的所有内容配置了 Spring 自动装配。
当我使用注释自动装配其他所有内容时:
@Autowired private SomeObject someObject;
它工作得很好。
但是当我尝试这个时:
@Autowired private Properties messages;
使用此配置:
<bean id="mybean" class="com.foo.MyBean" >
<property name="messages">
<util:properties location="classpath:messages.properties"/>
</property>
</bean>
我收到错误消息(仅相关行):
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mybean' defined in class path resource [application.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'messages' of bean class [com.foo.MyBean]: Bean property 'messages' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Wheras,如果我用一个很好的老式 setter 方法尝试它,Spring 会非常高兴地连接它:
public void setMessages(Properties p) { //this works
this.messages = p;
}
尝试自动装配属性对象时我做错了什么?