Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does child bean automatically become prototype if parent bean defined as prototype

Tags:

java

spring

In spring framework, if I have one bean defined as scope "protoype" and it is a parent to another bean. Does child bean automatically become prototype?

Example:

<bean id="a" class="..." scope="prototype"/>
<bean id="b" class="..." parent="a"/>

What will be the scope for b?

like image 485
kamoor Avatar asked Dec 16 '14 20:12

kamoor


3 Answers

I believe it will be a singleton since it is the default scope.

The remaining settings are always taken from the child definition: depends on, autowire mode, dependency check, singleton, scope, lazy init.

like image 191
csrcordeiro Avatar answered Oct 17 '22 04:10

csrcordeiro


As specified in doc : http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-child-bean-definitions

A child bean definition inherits constructor argument values, property values, and method overrides from the parent, with the option to add new values. Any initialization method, destroy method, and/or static factory method settings that you specify will override the corresponding parent settings.

The remaining settings are always taken from the child definition: depends on, autowire mode, dependency check, singleton, scope, lazy init.

like image 43
Semih Eker Avatar answered Oct 17 '22 05:10

Semih Eker


From the Spring reference:

3.6. Bean definition inheritance. The remaining settings will always be taken from the child definition: depends on, autowire mode, dependency check, singleton, scope, lazy init.

Therefore, t will not inherit the parent's scope

like image 2
Erik Schmiegelow Avatar answered Oct 17 '22 06:10

Erik Schmiegelow