在程序员生涯当中,提到最多的应该就是SSH三大框架了。作为第一大框架的Spring框架,我们经常使用。
然而在使用过程中,遇到过很多的常见异常,我在这里总结一下,大家共勉。
一、找不到配置文件的异常
[plain] view
plaincopy
- org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML
- document from class path resource [com/herman/ss/controller]; nested exception is java.io.FileNotFoundException:
- class path resource [com/herman/ss/controller] cannot be opened because it does not exist
解释:这个的意思是说,没有找配置文件为controller的xml,修改一下配置文件名字即可。
[html] view
plaincopy
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:com/herman/ss/config/testAjax.xml</param-value>
- </init-param>
二、在xml中配置的命名空间找不到对应的Schema的异常
[plain] view
plaincopy
- nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict,
- but no declaration can be found for element ‘util:list‘.
xmlns:util="http://www.springframework.org/schema/util" 去掉,因为schema中不存在util命名
三、找不到jackson.jar的异常
[plain] view
plaincopy
- StandardWrapper.Throwable
- java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonProcessingException
缺少jackson的jar包,导入jackson-all-1.9.5.jar即可
四、bean不是唯一的异常
[plain] view
plaincopy
- org.springframework.beans.factory.NoUniqueBeanDefinitionException:
- No qualifying bean of type [com.herman.ss.pojo.Person] is defined:
- expected single matching bean but found 7: person0,person1,person2,person3,person4,person5,person6
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:313)
- at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:985)
- at com.herman.ss.test.Test0.test1(Test0.java:35)
- at com.herman.ss.test.Test0.main(Test0.java:111)
这个异常是说,一个类配置了多个bean之后,我们还在使用ctx.getBean(Person.class);方法,即根据bean的类映射去获取bean对象。这个时候返回的bean对象不是唯一的,有多个bean对象。解决方法,就是根据bean的id去获取bean对象。
五、缺少日志jar包
[plain] view
plaincopy
- java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
这个问题是说,项目中缺少spring依赖的jar包文件。解决方案:加入commons-logging-1.1.3.jar即可。
六、找不到bean异常
[plain] view
plaincopy
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘filter2‘ is defined
这个问题是说,项目中找不到name为filter2的bean。说白了就是在applicationContext.xml中找不到id为filter2的bean,配置一下即可。
六、缺少spring-webmvc-4.0.6.RELEASE.jar包
[plain] view
plaincopy
- 严重: Error loading WebappClassLoader
- context: /Struts_Spring_Project
- delegate: false
- repositories:
- /WEB-INF/classes/
- ----------> Parent Classloader:
- [email protected]
- org.springframework.web.servlet.DispatcherServlet
- java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
解决方案:在项目中加入spring的mvc架包即可。如我的spring版本为4.0.6的,那么就把spring-webmvc-4.0.6.RELEASE.jar添加进去即可。
七、缺少spring-aop-4.0.6.RELEASE.jar包
[plain] view
plaincopy
- java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
- java.lang.ClassNotFoundException: org.springframework.aop.TargetSource
解决方案:在项目中加入spring的aop架包即可。如我的spring版本为4.0.6的,那么就把spring-aop-4.0.6.RELEASE.jar添加进去即可。
八、缺少spring-expression-4.0.6.RELEASE.jar包
[plain] view
plaincopy
- java.lang.NoClassDefFoundError: org/springframework/expression/ExpressionParser
- java.lang.ClassNotFoundException: org.springframework.expression.ExpressionParser
解决方案:在项目中加入spring的expression架包即可。如我的spring版本为4.0.6的,那么就把spring-expression-4.0.6.RELEASE.jar添加进去即可。
九、bean的名字name或者id或者别名alias已经存在
[plain] view
plaincopy
- org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
- Configuration problem: Bean name ‘a‘ is already used in this <beans> element
解决方法:把重复的名字改个名字即可。
十、bean的自动加载找不到相对应的bean问题
[plain] view
plaincopy
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.yyc.ym.biz.YycBiz] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
解决方法:在配置文件中的<beans>根节点下加default-autowire="byName" default-lazy-init="true"或者<context:component-scan base-package="com.xxx.dao.*"></context:component-scan>包下面用*匹配