简体   繁体   English

覆盖子bean中的spring范围

[英]Override a spring scope in a child bean

I have defined a spring bean extending another bean defined as singleton. 我定义了一个Spring bean,扩展了另一个定义为singleton的bean。 Which means this: 这意味着:

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

Now, I wonder if I could define the scope "request" in this bean. 现在,我想知道是否可以在此bean中定义范围“ request”。 I know that the child bean inherits the scope of the parent, but I'm not sure that this could logically work. 我知道子bean继承了父bean的作用域,但是我不确定这在逻辑上是否可行。 When I tested this, Spring spring generated the exception below: 当我对此进行测试时,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? ...

So, I wonder if I could do such action. 因此,我想知道我是否可以执行此操作。 And, if the definition of a scoped bean solve the problem ? 并且,如果定义范围的bean可以解决问题?

Thanks in advance for your answers.. 预先感谢您的回答。

Quote from documentation : 引用文档

A child bean definition inherits constructor argument values, property values, and method overrides from the parent, with the option to add new values. 子bean定义从父对象继承构造函数参数值,属性值和方法替代,并可以选择添加新值。 Any initialization method, destroy method, and/or static factory method settings that you specify will override the corresponding parent settings. 您指定的任何初始化方法,destroy方法和/或静态工厂方法设置都将覆盖相应的父设置。

The remaining settings are always taken from the child definition: depends on, autowire mode, dependency check, singleton, scope, lazy init. 其余设置始终从子定义中获取:依赖项,自动装配模式,依赖项检查,单例,作用域,惰性初始化。

So, your bean IS "request" scoped, but a "request" scope only makes sense in a web environment. 因此,您的bean是“请求”作用域的,但是“请求”作用域仅在Web环境中才有意义。 See here the documentation of "request" scope. 这里的“请求”范围的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM