好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring框架十一种常见异常的解决方法汇总

在程序员生涯当中,提到最多的应该就是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://HdhCmsTestspringframework.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测试数据mons.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

查看更多关于Spring框架十一种常见异常的解决方法汇总的详细内容...

  阅读:19次