Spring 菜鸟教程 异常 集锦

异常1.空指针异常

异常信息摘要:

java.lang.NullPointerException
    at com.jege.framework.service.UserServiceTest.get(UserServiceTest.java:36)

异常信息说明:server接口没有注入
解决思路:可能的原因:

1.xml版本,没有写set方法,spring配置文件配置注入写的有问题
2.注解版本,没有写@Autowired或者@Resource
3.测试类上面没有标注
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")

异常2.bean的名字name或者id已经存在

异常信息摘要:

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

异常信息说明:helleService bean对象已经存在
解决思路:把重复的名字改一下:


异常3.web项目找不到默认配置文件

异常信息摘要:

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML   
document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException:  
 class path resource [WEB-INF/applicationContext.xml] cannot be opened because it does not exist  

异常信息说明:web项目默认会在WEB-INF下找applicationContext.xml配置文件
解决思路:在web.xml配置一个上下文属性:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

异常4.命名空间配置错误

异常信息摘要:

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 'aop:confog'.  

异常信息说明:没有找到aop:confog的配置
解决思路:配置好schema的aop命名空间

xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

异常5.bean不是唯一的异常

异常信息摘要:

org.springframework.beans.factory.NoUniqueBeanDefinitionException:   
No qualifying bean of type [com.jege.ssh.dao.IUserDao] is defined:   
expected single matching bean but found 2: UserDao1,UserDao2
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:313)  

异常信息说明:注入的接口有2个实现
解决思路:去掉一个实现类,一个接口最好只有一个实现


异常6.bean没有定义,不存在

异常信息摘要:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userDao' is defined  

异常信息说明:userDao没有定义
解决思路:在applicationContext.xml中找不到id为userDao的bean,配置一下或者注解没有标注@Repository


异常7.server接口没有注入

异常信息摘要:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.jege.framework.service.UserServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.jege.framework.service.IUserService com.jege.framework.service.UserServiceTest.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.jege.framework.service.IUserService] 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)}
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.jege.framework.service.IUserService com.jege.framework.service.UserServiceTest.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.jege.framework.service.IUserService] 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)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.jege.framework.service.IUserService] 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)}

异常信息说明:server接口没有注入
解决思路:可能的原因:

1.xml版本,没有写set方法,spring配置文件配置注入有问题
2.注解版本,<context:component-scan base-package="com.jege.framework包写错" />

异常8.aspectjweaver-1.8.0.jar spring4要使用高版本


如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
您的支持将鼓励我继续创作!谢谢!
微信打赏
支付宝打赏

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值