Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Spring: ref local vs ref bean

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between <ref local=".."/> and <ref bean=".."/>?
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Spring docs,




Specifying the target bean by using the bean attribute of the ref tag is the most general form, and will allow creating a reference to any bean in the same BeanFactory/ApplicationContext (whether or not in the same XML file), or parent BeanFactory/ApplicationContext. The value of the bean attribute may be the same as either the id attribute of the target bean, or one of the values in the name attribute of the target bean.

<ref bean="someBean"/>
Specifying the target bean by using the local attribute leverages the ability of the XML parser to validate XML id references within the same file. The value of the local attribute must be the same as the id attribute of the target bean. The XML parser will issue an error if no matching element is found in the same file. As such, using the local variant is the best choice (in order to know about errors are early as possible) if the target bean is in the same XML file.

<ref local="someBean"/>


 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is only effective if you use more than 1 configuration file for your bean container in bigger application.
There might be a good explanation in spring documentation, but I am not very familiar with it yet.
Correcter wording and more details to follow.

just my 2 euro cent

Axel
 
Amer Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bean can be given more than one name so that it can be referred by using different aliases.When using local attribute it means <ref> tag only looks at the bean's id and never at any of its aliases .When injecting a bean by name,the bean attribute of the <ref> tag is used.

<bean id ="injRef" class="com.myPac.ClassA">
<property name="abc">
<ref bean ="good"/>
</property>
</bean>
<bean id="abc" name ="good" class=com.myPac.ClassB"/>

Here 'abc'bean is given an alias using the 'name' attribute and it is injected into intRef bean by uing this alias in conjuction with the bean attribute of the <ref> tag.
[ May 16, 2005: Message edited by: Amer Khan ]
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!