wood burning stoves 2.0*
The moose likes Spring and the fly likes what will be bean's scope? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "what will be bean Watch "what will be bean New topic
Author

what will be bean's scope?

shashidhar kumar
Greenhorn

Joined: Jan 09, 2013
Posts: 4
Hi,

This is the definition in xml file, spring reads through ApplicationContext.
<bean id="simpleAction" class="com.sapient.SimpleAction"/>
Here, since scope is not defined, it's scope will be singleton.

In SimpleAction java class, using annotations, it is defined here as well:
@Component
@Scope("prototype")

Do the SimpleAction bean get prototype scope, mentioned in java file ?

Thanks,
Shashi
Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 122

It will end up as a 'prototype'.

The duplicate setting in XML can override the setting in the annotation. In this case, the XML setting did not override the scope set in the annotation.
shashidhar kumar
Greenhorn

Joined: Jan 09, 2013
Posts: 4
Thanks Kathleen, Could you please explain, how bean gets 'prototype' scope ? If you have reference which I can take look into, please provide it.

When does duplicate setting in XML overwrite the setting in annotation ?

Further, the following two lines are placed at top of applicationContext.xml file, before bean definitions (the file contains bean definitions, used by ApplicationContext to create itself).

<context:annotation-config />
<context:component-scan base-package="com.project.struts" />

Do the position of these two lines matter ?
Pyla Rao
Ranch Hand

Joined: Jul 10, 2012
Posts: 50
Hi Kumar,


It is intimate beans in this container are annotations supported.


If specify like above the beans are in package are automatically registered.We can give more then one package by comma separated.
Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 122

shashidhar kumar wrote:Thanks Kathleen, Could you please explain, how bean gets 'prototype' scope ? If you have reference which I can take look into, please provide it.

When does duplicate setting in XML overwrite the setting in annotation ?




Let me clarify and correct my statement.

Below is the only documentation I found: from page 84 (or 108) of the Spring Framework documentation pdf in the spring-framework-3.0.7.RELEASE-with-docs download, or at 3.9 Annotation-based container configuration.


Annotation injection is performed before XML injection, thus the latter configuration will
override the former for properties wired through both approaches.


In terms of injection, above implies that when a property is able to find a value through both annotation and xml configuration, 2 settings will be done, whereby the annotation setting is the second, and therefore the final value.

With regards to if this applies to the 'scope', we may need to test ourselves. I dont find the above statement clear if it applies to the attributes like 'scope'.

shashidhar kumar wrote:
<context:annotation-config />
<context:component-scan base-package="com.project.struts" />

Do the position of these two lines matter ?


There is no effect on the order of which the above-mentioned elements are declared in the xml file.
shashidhar kumar
Greenhorn

Joined: Jan 09, 2013
Posts: 4

Thanks Kathleen, It very much helped in my understanding.

I am trying to implement it with a sample code and will tell the execution behaviour.
shashidhar kumar
Greenhorn

Joined: Jan 09, 2013
Posts: 4

Here is one more update.

The bean in xml was not given "scope" explicitly, i.e., it is like this:
<bean id="simpleAction" class="com.sapient.SimpleAction"/>

In java class, annotation was given scope explicitly.
i.e.
@Component
@Scope("prototype")

But finally, the scope of the bean is singleton from its behaviour.
That might mean, when bean is defined in the xml file, it overwrites all the properties specified elsewhere that are applicable for the bean.

Thanks,
Shashidhar.
Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 122

Thanks for sharing your findings.
 
 
subject: what will be bean's scope?