Spring bean的作用範圍

2021-10-01 08:53:22 字數 983 閱讀 8870

bean的作用範圍可以通過bean標籤的scope屬性來設定

scope屬性的可選的值

1)singleton 單列(預設)

2)prototype 多例

3)request 作用於web應用請求範圍

4)session 作用於web應用會話範圍

5)global-session 作用於集群環境的會話範圍(全域性會話範圍),當不是集群環境時,就是session

1)singleton 單列(預設)

bean.xml檔案

"accountservice"

class

="com.zl.service.impl.accountserviceimpl"

>

<

/bean>

bean呼叫類:

public

static

void

main

(string[

] args)

執行結果:

1)prototype 多例

bean.xml檔案

"accountservice"

class

="com.zl.service.impl.accountserviceimpl" scope=

"prototype"

>

<

/bean>

bean呼叫類:

public

static

void

main

(string[

] args)

執行結果:

Spring bean作用範圍

bean的作用範圍通過scope調整 取值描述 singleton 單例的 預設值 prototype 多例的request 作用於web應用的請求範圍 session 作用於web應用的會話範圍 global session 作用於集群環境的會話範圍 全域性會話範圍 當不是集群環境時,它就是ses...

Spring Bean的作用域(作用範圍)

文章目錄 scope 1.註解方式配置 1.1 測試singleton 1.2 測試prototype 2.xml方式配置在spring中使用 scope來表示乙個bean定義對應產生例項的型別,也可以說是對應例項的作用範圍。spring中 指定scope的方法分以下兩種 採用xml配置方式時,可以...

Spring Bean的作用域

bean的作用域,常用的有兩種,單例singleton 多例prototype 預設情況下,bean都是單例的singleton。在容器初始化的時候就被建立,就這麼乙份。1 單例模式 例如 測試 package com.lynn.spring.test import static org.junit...