• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Using ref attribute/tag to link to other beans

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I just learned how to get reference to other beans using the ref attribute of property/constructor-arg tags. Also I understand ref can be a tag inside the body of a property/constructor-arg.

My question is, when we use ref as tag in the body, we can specify the particular bean we want to reference to using the bean or local attributes of the ref tag.

When local is used XML validation occurs to check if a bean with that id exists or not. My inference is, when using bean attribute, it does not do XML validation.

1. I found XML validation occurs to check if a bean with that id exists or not, even when using bean attribute.
If 1. above is wrong, why this behavior? Why would bean attribute not be XML validated?
If this bean was in some other bean configuration file, how would Spring find it/or how do I reference a bean in other file?

Thanks,
Suhas.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My inference is, when using bean attribute, it does not do XML validation


That's right.

I found XML validation occurs to check if a bean with that id exists or not, even when using bean attribute.


Are you sure ? Which excpetion was thrown ? If you are using an ApplicationContext, your bean will be automatically instanciated (unless you use lazy-init), and an instanciation error will occur, but not an xml parsing error.

When using "local", Spring will check the presence of the referenced bean during the xml parsing. Whether you use that bean in your application or not, it will check that it is properly declared. If not, you'll get an XmlBeanDefinitionStoreException.

When using "bean", Spring will check the presence of the referenced bean when the bean referencing it is instanciated, not during the xml parsing. Which means that even if the bean does not exist, and you do not use the bean referencing an unknown bean, your application will work. For example :

If mickDo does not exist, and the mick bean is not used, the application will work. If the mick bean is instanciated, BeanCreationException will be thrown.
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Christopher,

I checked it out again. Thanks for pointing that out.

So I used bean attribute of the ref tag and hit the BeanCreationException

When I used the local attribute, it threw XmlBeanDefinitionStoreException, even when I had that bean declared in another bean config and had that loaded prior to the one that made the ref local reference.

Thanks again.
Suhas.
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just some thought that I had.....

Why not just have one attribute bean, that would by default look locally and if it does not find it locally search elsewhere in the context?
I mean, it could be a little confusing given this situation.
The same bean (same bean id) exists in more than one config files. However, as far as I know, the bean that is loaded last (order specified in the array of config files while creating ClassPathXmlApplicationContext) will be stored in the Map against the unique id.
When I use bean attribute, I get the expected behavior - the bean from the last config file is used, the value of which is last placed in the bean map
When I use local attribute, I expect the bean from the same XML to be used. But NO! Again the bean from the last loaded config file is used (which I understand is again because the last bean overwrites the value in bean map). So if using local attribute does not do anything more than throw a XmlBeanDefinitionStoreException, what is the point in having it? I would expect the local attribute to use the bean from the same file it is declared, when overriding beans exist.


Thoughts?!!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that the attribute name "local" is confusing. The XML Schema says :
"local" : The name of the referenced bean. The value must be a bean ID and thus can be checked by the XML parser. This is therefore the preferred technique for referencing beans within the same bean factory XML file.
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well...even if spec says that it checks whether the referenced bean exists in the same XML or not, all it does is give user a warning.
There is no compilation issue or anything in Spring that enforces the bean declaration to exist in the same XML. And when overriding beans exist, it might totally lose its significance.

Anyways..its a minor thing.

Thanks for your help.

Suhas.
 
reply
    Bookmark Topic Watch Topic
  • New Topic