Spring10种常见异常解决方法

简介: 在程序员生涯当中,提到最多的应该就是SSH三大框架了。作为第一大框架的Spring框架,我们经常使用。<br><p>然而在使用过程中,遇到过很多的常见异常,我在这里总结一下,大家共勉。</p> <p><strong>一、找不到配置文件的异常</strong><br></p> <pre code_snippet_id="442982" snippet_file_name="blog_20
在程序员生涯当中,提到最多的应该就是SSH三大框架了。作为第一大框架的Spring框架,我们经常使用。

然而在使用过程中,遇到过很多的常见异常,我在这里总结一下,大家共勉。

一、找不到配置文件的异常

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,修改一下配置文件名字即可。
<init-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:com/herman/ss/config/testAjax.xml</param-value>
</init-param>
二、在xml中配置的命名空间找不到对应的Schema的异常
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的异常
StandardWrapper.Throwable
java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonProcessingException

缺少jackson的jar包,导入jackson-all-1.9.5.jar即可

四、bean不是唯一的异常

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包

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异常

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包

严重: 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包

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包

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已经存在

org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
 Configuration problem: Bean name 'a' is already used in this <beans> element

解决方法:把重复的名字改个名字即可。

十、bean的自动加载找不到相对应的bean问题

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>包下面用*匹配 如有不懂,疑问或者欠妥的地方,请加QQ群:135430763   进行反馈,共同学习!

目录
相关文章
在使用SSH+Spring开发webservice ,报的一些异常及处理方法
在使用SSH+Spring开发webservice ,报的一些异常及处理方法
201 0
在使用SSH+Spring开发webservice ,报的一些异常及处理方法
|
4月前
|
安全 前端开发 Java
Spring Security 自定义异常失效?从源码分析到解决方案
Spring Security 自定义异常失效?从源码分析到解决方案
|
1月前
|
Java 开发者 Spring
【Java】Spring循环依赖:原因与解决方法
【Java】Spring循环依赖:原因与解决方法
39 0
|
1月前
|
前端开发 Java 数据安全/隐私保护
Spring Boot3自定义异常及全局异常捕获
Spring Boot3自定义异常及全局异常捕获
45 1
|
6月前
|
Java 微服务 Spring
【Java异常】Spring boot启动失败@org.springframework.beans.factory.annotation.Autowired(required=true)
【Java异常】Spring boot启动失败@org.springframework.beans.factory.annotation.Autowired(required=true)
52 0
|
5月前
|
Java Spring
Idea spring boot cannot autowired 解决方法
Idea spring boot cannot autowired 解决方法
|
7月前
|
Java Spring
Spring事务异常不回滚的原因
Spring事务异常不回滚的原因
270 0
|
9月前
|
XML JSON SpringCloudAlibaba
Spring Boot如何优雅实现结果统一封装和异常统一处理
当下基于`Spring Boot`框架开发的系统几乎都是前后端分离的,也都是基于`RESTFUL`风格进行接口定义开发的,意味着前后端开发大部分数据的传输格式都是json,因此定义一个统一规范的数据格式返回有利于前后端的交互与UI的展示
198 0
|
JSON 安全 Java
Spring Boot之全局异常处理:404异常为何捕获不到?
Spring Boot之全局异常处理:404异常为何捕获不到?
1216 0
Spring Boot之全局异常处理:404异常为何捕获不到?
|
11月前
|
缓存 前端开发 NoSQL
Spring Boot2.x-13前后端分离的跨域问题解决方法之Nginx
Spring Boot2.x-13前后端分离的跨域问题解决方法之Nginx
255 0