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  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配置一个上下文属性:

param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xmlparam-value>
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要使用高版本


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

你可能感兴趣的:(Spring,异常集锦)