Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property ‘per’ of bean class [com.zyq.service.impl.PersonServiceImpl]: Bean property ‘per’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
报错原因:
ApplicationContext.xml
文件中
<bean id="per" class="com.zyq.dao.impl.PersonDaoImpl"></bean>
<!-- PersonServiceImpl类中引用了personDao类 -->
<bean id="personService" class="com.zyq.service.impl.PersonServiceImpl">
<property name="per" ref="per"></property>
</bean>
</beans>
property
属性下name
名称与Impl
实现类中的setter()
方法入参类型不对应:
<property name="per" ref="per"></property>
此处name="per"
应该与
public void setPersonDao(PersonDao person) {
this.personDa = person;
}
中PersonDao
对应. 应改为name="personDao"
或name="PersonDao"
.
实现类:
public class PersonServiceImpl implements PersonService {
//原始new方法
// PersonDao personDao = new PersonDaoImpl();
//IOC方法 报空指针因为没有set方法 且PersonServiceImpl注入时要ref一下personDao类
private PersonDao personDa;
@Override
public void say() {
System.out.println("PersonService的say方法");
personDa.eat();
}
public void setPersonDao(PersonDao person) {
this.personDa = person;
}
}
若此处报空指针异常错误,则大概率是Impl
层没加Setter()
方法.