在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结,希望遇见类似错误的同学们共勉一下。
1. 错误一
Error creating bean with name 'helloServiceImpl' defined in class path resource [spring-service.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'helloDao' of bean class [www.csdn.spring.service.impl.HelloServiceImpl]: Bean property 'helloDao' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'helloDao' of bean class
这类错误是:一般都是创建了一个dao的spring文件比如spring-dao有创建了一个service的spring文件,在spring-service.xml中引用dao的中定义的id名,导致的错误,疏忽是:写service实现类的时候忘记了写对应dao的setter方法,即所谓的依赖注入
比如:
private HelloDao helloDao;
//set依赖注入很重要,不写会报错,不能读写helloDao这一属性
publicvoid setHelloDao(HelloDao helloDao) {
System.out
.println("控制反转:应用程序本身不在负责创建helloDao对象,而是由spring容器负责创建、管理、维护,这样控制权转移,称为反转。"
+ "可以通过依赖注入方式注入该HelloDao对象");
this.helloDao = helloDao;
}
2. 错误二
Configuration problem: Failed to import bean definitions from relative location [spring-dao.xml]Offending resource: class path resource [spring.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 9 in XML document from class path resource [spring-dao.xml] is invalid; nested exception is org.xml.sax.SAXParseException: Open quote is expected for attribute "{1}" associated with an element type "scope".
Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 9 in XML document from class path resource [spring-dao.xml] is invalid; nested exception is org.xml.sax.SAXParseException: Open quote is expected for
Caused by: org.xml.sax.SAXParseException: Open quote is expected for attribute "{1}" associated with an element type "scope".
这种错误是马虎的错误,在对应的spring的配置文件中,bean标签的scope属性忘了加引号,在配置文件中国不会报错,但是在运行的时候就会出这样的错,一般导致错误的原因是复制的时候疏忽了引号,直接将原来的引号覆盖了,导致了最后该属性没有引号。
scope="prototype">
错误的写成:
bean id="helloDaoImpl" class="www.csdn.spring.dao.impl.HelloDaoImpl"
scope=prototype>
3. 错误三
No bean named 'helloServiceImp' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition
这种报错但是没有Caused by语句的错误一般都是使用的时候单词写错了,这里写错的地方是在java类中,类中引用id的时候写错了单词;比如这里的错,注意下面的红色文字:
HelloService helloService2 = (HelloService) context.getBean("helloServiceImp",HelloServiceImpl.class);
眼尖的哥们估计都看出来了这两个单词写的不一样,获取bean的方法中引用的id少写了一个“i”,导致spring容器在读取的时候不能识别。以后注意细心就好。
4. 错误四
Error creating bean with name 'helloServiceImpl' defined in class path resource [spring-service.xml]: Cannot resolve reference to bean 'helloDaoImp' while setting bean property 'helloDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloDaoImp' is defined
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloDaoImp' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition
这种也是单词写错的情况,与上面不同的是这种情况给出了Caused by语句,让你更清楚错误发生的原因,英文还差不多的哥们或者有一定编程经验的人员一看Caused by语句就应该能判断出什么错误;这里错误原因明显才、指出每一个名字为'helloDaoImp'的bean,或名字为'helloDaoImp'的bean未定义,这种错也好好找,一般都是互相引用的spring配置文件马虎出的错,下面一个例