• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what will be bean's scope?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for sharing your findings.
 
reply
    Bookmark Topic Watch Topic
  • New Topic