spring bean的作用域

ScopeDescription

singleton

(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.

prototype

Scopes a single bean definition to any number of object instances.

request

Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.

session

Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

application

Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.

websocket

Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

singleton  单例模式

prototype 原型模式

 <bean id="user3" class="com.wt.pojo.User" scope="prototype">
        <constructor-arg name="name" value="gg"/>
        <constructor-arg name="age" value="45"/>
</bean>

spring 默认是单例机制

原型模式:每次从容器中get的时候,都会获取新的对象

原文地址:https://www.cnblogs.com/wt7018/p/13337700.html