首页 / 软件开发 / JAVA / spring入门(16) spring常见错误总结
spring入门(16) spring常见错误总结2013-07-19 csdn 史国旭在学习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 forCaused by: org.xml.sax.SAXParseException: Open quote is expected for attribute "{1}" associated with an element type "scope".这种错误是马虎的错误,在对应的spring的配置文件中,bean标签的scope属性忘了加引号,在配置文件中国不会报错,但是在运行的时候就会出这样的错,一般导致错误的原因是复制的时候疏忽了引号,直接将原来的引号覆盖了,导致了最后该属性没有引号。<bean id="helloDaoImpl" class="www.csdn.spring.dao.impl.HelloDaoImpl"scope="prototype"></bean><!--p-->错误的写成:bean id="helloDaoImpl" class="www.csdn.spring.dao.impl.HelloDaoImpl"scope=prototype></bean>3. 错误三No bean named "helloServiceImp" is definedat org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition这种报错但是没有Caused by语句的错误一般都是使用的时候单词写错了,这里写错的地方是在java类中,类中引用id的时候写错了单词;比如这里的错,注意下面的红色文字:HelloService helloService2 = (HelloService) context.getBean("helloServiceImp",HelloServiceImpl.class);<bean id="helloServiceImpl" class="www.csdn.spring.service.impl.HelloServiceImpl" scope="singleton" lazy-init="false"><property name="helloDao" ref="helloDaoImpl" /></bean>眼尖的哥们估计都看出来了这两个单词写的不一样,获取bean的方法中引用的id少写了一个“i”,导致spring容器在读取的时候不能识别。以后注意细心就好。