繁体   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