在程序員生涯當中,提到最多的應該就是ssh三大框架了。作為第一大框架的spring框架,我們經常使用。
然而在使用過程中,遇到過很多的常見異常,我在這里總結一下,大家共勉。
一、找不到配置文件的異常
1
2
3
|
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,修改一下配置文件名字即可。
1
2
3
4
|
<init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:com/herman/ss/config/testajax.xml</param-value> </init-param> |
二、在xml中配置的命名空間找不到對應的schema的異常
1
2
3
|
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的異常
1
2
|
standardwrapper.throwable java.lang.noclassdeffounderror: org/codehaus/jackson/jsonprocessingexception |
缺少jackson的jar包,導入jackson-all-1.9.5.jar即可
四、bean不是唯一的異常
1
2
3
4
5
6
7
|
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包
1
2
|
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異常
1
|
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包
1
2
3
4
5
6
7
8
9
|
嚴重: error loading webappclassloader context: /struts_spring_project delegate: false repositories: /web-inf/classes/ ----------> parent classloader: org.apache.catalina.loader.standardclassloader @b33d0a 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包
1
2
|
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包
1
2
|
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已經存在
1
2
|
org.springframework.beans.factory.parsing.beandefinitionparsingexception: configuration problem: bean name 'a' is already used in this <beans> element |
解決方法:把重復的名字改個名字即可。
十一、bean的自動加載找不到相對應的bean問題
1
|
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>包下面用*匹配
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/qq_26562641/article/details/70883946