Tuesday, May 29, 2012

difference between bean id and name attribute in Spring

1. The id gets automatically and stricter validated by the XML parser since it has a special meaning in XML. So for example you can't add twice the same id into the XML, the parser would complain. This helps to edit the file since you can see an error in an XML editor. That's no possible for name. Spring can only complain at runtime about duplicate name.

2. Also, you cannot use certain characters in an XML id attribute: if you're using URLs as bean names, as is common in Spring MVC, then you'll need to use name="/path/somecontroller" instead of id, since the slashes are not legal characters to use for an XML id. That's actually the most common reason to use name instead of id. Others are if you need to specify several aliases using a comma seperated list in the name attribute: comma's are also not allowed inside an XML id.

3 comments:

Anonymous said...

Thanks for the valuable info Shekar.

Anonymous said...

Good Information

Anonymous said...
This comment has been removed by a blog administrator.