Spring——bean作用域

目录

 

BeanScopes

单例模式(Spring默认机制)

原型模式

BeanScopes

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.

单例模式(Spring默认机制)

Java中单例模式定义:“一个类有且仅有一个实例,并且自行实例化向整个系统提供。”

    <bean id="user" class="com.lys.pojo.User" p:name="lys" p:age="18" 
    scope="singleton"/>

原型模式

每次从容器中get的时候,都会产生一个新的对象

    <bean id="user" class="com.lys.pojo.User" p:name="lys" p:age="18" 
    scope="prototype"/>

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值