小談spring中的scope屬性

2021-07-31 06:37:31 字數 1170 閱讀 3761

在一次做專案時遇到了這樣的一則警告:invocation of destroy method 'close' failed on bean with name 'sqlsession'

[html]view plain

copy

print?

<

bean

id="sqlsessiontemplate"

class="org.mybatis.spring.sqlsessiontemplate"

scope="prototype"

>

[html]view plain

copy

print?

<

constructor-arg

index="0"

>

<

refbean="sqlsessionfactory"

/>

constructor-arg

>

lt;/bean>

後來得知,spring + mybatis 配置中如果缺少 scope="prototype"屬性會報
warn:invocation of destroy method 'close' failed on bean with name 'sqlsession'
由此,那麼就來小談一下spring中的scope屬性

spring 預設scope 是單例模式,這樣只會建立乙個action物件。每次訪問都是同乙個action物件,資料不安全

struts2 是要求每次次訪問都對應不同的action,scope="prototype" 可以保證當有請求的時候都建立乙個action物件

可以利用容器的scope="prototype"來保證每乙個請求有乙個單獨的action來處理, 避免struts中action的執行緒安全問題

scope="prototype"的bean容器在接受到該型別的物件的請求的時候,會每次都重新生成乙個新的物件給請求方;

scope="singleton"的bean是由容器來保證這種型別的bean在同乙個容器內只存在乙個共享例項

spring中的屬性scope

之前在開發過程中遇到了乙個問題,當request發起下乙個請求的時候,action中的屬性值並沒有清除,而是繼續採用上次使用過的引數值進行查詢並響應,導致資料查詢不真實或失敗。對此,有同事告訴我說,需要在spring配置檔案的bean中新增屬性scope prototype。結果還真的有用。下面是我...

Spring中的 scope註解

預設是單例模式,即scope singleton 另外scope還有prototype request session global session作用域。scope prototype 多例 scope預設是單例模式 singleton 如果需要設定的話 scope prototype 1.sin...

Spring中的 scope註解

預設是單例模式,即scope singleton 另外scope還有prototype request session global session作用域。scope prototype 多例 scope預設是單例模式 singleton 如果需要設定的話 scope prototype 1.sin...