簡體   English   中英

覆蓋子bean中的spring范圍

[英]Override a spring scope in a child bean

我定義了一個Spring bean,擴展了另一個定義為singleton的bean。 這意味着:

<bean id="aChildBean" parent="aParentBean">
   <!-- ......->
</bean>

現在,我想知道是否可以在此bean中定義范圍“ request”。 我知道子bean繼承了父bean的作用域,但是我不確定這在邏輯上是否可行。 當我對此進行測試時,Spring spring生成了以下異常:

Error creating bean with name 'aChildBean': Scope 'request' is not active for the
 current thread; consider defining a scoped proxy for this bean if you intend to refer
to it from a singleton; nested exception is java.lang.IllegalStateException: No thread
bound request found: Are you referring to request attributes outside of an actual web
request, or processing a request outside of the originally receiving thread? ...

因此,我想知道我是否可以執行此操作。 並且,如果定義范圍的bean可以解決問題?

預先感謝您的回答。

引用文檔

子bean定義從父對象繼承構造函數參數值,屬性值和方法替代,並可以選擇添加新值。 您指定的任何初始化方法,destroy方法和/或靜態工廠方法設置都將覆蓋相應的父設置。

其余設置始終從子定義中獲取:依賴項,自動裝配模式,依賴項檢查,單例,作用域,惰性初始化。

因此,您的bean是“請求”作用域的,但是“請求”作用域僅在Web環境中才有意義。 這里的“請求”范圍的文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM