Bean property 'users' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
错误原因:users作为属性名,写错了,没有匹配上RolesProps的属性名,改成props就没问题了。
<bean id="rolesprops" class="springidol.RolesProps">
<property name="roleName" value="超级管理props"/>
<property name="users">
<props>
<prop key="USER">王武</prop>
<prop key="AGE">19</prop>
</props>
</property>
<property name="nullprop"> <null/> </property>
</bean>
/**
* Properties注入
*/
public class RolesProps {
private String roleName;
private Properties props;
private String nullprop;
public String getNullprop() {
return nullprop;
}
public void setNullprop(String nullprop) {
this.nullprop = nullprop;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
public static void props(){
ApplicationContext factory = new ClassPathXmlApplicationContext("spring-beanstudy.xml");
RolesProps obj = (RolesProps) factory.getBean("rolesprops");
System.out.println(obj.getRoleName());
System.out.println(obj.getNullprop());
System.out.println(obj.getProps().get("USER"));
System.out.println(obj.getProps().get("AGE"));
}