Spring Bean作用域
默认情况下,所有的 Spring Bean 都是单例的,也就是说在整个 Spring 应用中, Bean 的实例只有一个
如果我们需要创建多个实例的对象,那么应该将 Bean 的 scope 属性定义为 prototype,如果 Spring 需要每次都返回一个相同的 Bean 实例,则应将 Bean 的 scope 属性定义为 singleton。(默认)
Spring 5 共提供了 6 种 scope 作用域
这里只说 单例 和 多例的配置
多例
<!-- User -->
<bean class="com.liu.c.User" id="user" scope="prototype">
<constructor-arg name="age" value="15"/>
<constructor-arg name="name" value="小张"/>