关于Spring遇到的各种问题

个人简介

  • 作者简介:大家好!我是yukki。
  • 个人主页:yukki.
  •  喜欢:🌈点赞🌈收藏🌈一键三连!
  • 共勉

1、出现报错

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.atguigu.spring.pojo.Student' available: expected single matching bean but found 2: studentOne,studentTwo
 

(1)可能是因为获取bean时,没有对应匹配的

(2)可能是因为获取bean时,没有指定哪一个bean

即:

* 若没有任何一个类型的bean,此时抛出异常:NoUniqueBeanDefinitionException
* 若有多个类型匹配的bean,此时抛出异常:NoUniqueBeanDefinitionException

2、出现错误

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.atguigu.spring.pojo.Person' available: expected single matching bean but found 2: studentOne,studentTwo
 

(1)可能是因为xml中的bean有多个,并不是唯一。

即:

*  如果组件类实现了接口,根据接口类型可以获取 bean 吗? 可以,前提是bean唯一

*  如果一个接口有多个实现类,这些实现类都配置了 bean,根据接口类型可以获取 bean 吗? 不行,因为bean不唯一

3、出现错误,空指针

 java.lang.NullPointerException
    at com.atguigu.spring.test.IOCByXMLTest.testDIFour(

(1)哪一行出现问题哪一个就是空指针

即:

配置文件可能有null,不是value=null.二者不一样。

4、出现报错

: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentFive' defined in class path resource [spring-ioc.xml]: Error setting property values; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'clazz' of bean class [com.atguigu.spring.pojo.Student]: Value of nested property 'clazz' is null

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentFive' defined in class path resource [spring-ioc.xml]: Error setting property values; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'clazz' of bean class [com.atguigu.spring.pojo.Student]: Value of nested property 'clazz' is null
 

 (1)把上一行注释去掉

即:

级联的方式,要保证提前为clazz属性赋值或者实例化  

5、出现错误

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'clazzInner' available

(1)不能获取IOC的内部bean

即:

内部bean,只能在当前bean的内部使用,不能直接通过IOC容器获取

6、

xception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController' defined in class path resource [spring-autowire-xml.xml]: Cannot resolve reference to bean 'userService' while setting bean property 'userService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [spring-autowire-xml.xml]: Cannot resolve reference to bean 'userDao' while setting bean property 'userDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userDao' available

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController' defined in class path resource [spring-autowire-xml.xml]: Cannot resolve reference to bean 'userService' while setting bean property

(1)缺少id="userDao"

7、

ava.lang.NullPointerException
    at com.atguigu.spring.controller.UserController.saveUser(UserController.java:22)
    at com.atguigu.spring.test.AutowireByXMLTest.testAutowire(AutowireByXMLTest.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

(1)空指针异常 

即:

在自动装配时 ,配置方式未指定。或者

autowire="default" or "no"相当于不设置

8、

xception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController' defined in class path resource [spring-autowire-xml.xml]: Unsatisfied dependency expressed through bean property 'userService'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.atguigu.spring.service.UserService' available: expected single matching bean but found 2: userService,user

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController' defined in class path resource [spring-autowire-xml.xml]: Unsatisfied dependency expressed through bean property 'userService'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.atguigu.spring.service.UserService' available: expected single matching bean but found 2: userService,user

a>若通过类型没有找到任何一个类型匹配的的bean,此时不装配,属性使用默认值
* b>若通过类型找到了多个类型匹配的bean,此时会抛出异常:NoUniqueBeanDefinitionException
* 总结:当使用byType实现自动化装配,IOC容器中有且只有一个类型匹配的bean能够为属性赋值 

9、

这里的NoSuchBeanDefinitionException可能是没有合适的bean进行装配 ,也就是自动装配的错 

 10、

Caused by: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

事物的只读操作的报错

11、

org.springframework.transaction.TransactionTimedOutException: Transaction timed out: deadline was Sun Nov 06 19:56:58 CST 2022

事物的超时报错,会实现回滚

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值