0
点赞
收藏
分享

微信扫一扫

Spring中Bean的作用范围

一条咸鱼的干货 2022-02-16 阅读 107
springjava

文章目录

Spring中Bean的作用范围

在这里插入图片描述

bug1:

在这里插入图片描述

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'user2' defined in class path resource [userBeans.xml]: Unsatisfied dependency expressed through constructor parameter 1: Ambiguous argument values for parameter of type [int] - did you specify the correct bean references as arguments?

通过构造函数参数1表示的依赖不满足:[int]类型参数的参数值不明确——你指定正确的bean引用作为参数了吗?

原因 :
原来是c命名空间,标签写成了p命令空间。

1、单例模式(Spring默认模式)

<bean id="user2" class="com.blackcat.pojo.User" c:name="白猫" c:age="1" scope="singleton"/>

2、原型模式:每次从容器中get的时候,都会产生一个新对象!

<bean id="user2" class="com.blackcat.pojo.User" c:name="白猫" c:age="1" scope="prototype"/>

3、其余的 request,session,application 这些只能在web开发中使用到。

建议:单线程都使用单例,多线程都使用多例,因为有线程安全问题。

举报

相关推荐

0 条评论