Results 1 to 5 of 5

Thread: What is difference between id and name in beans config xml?

Hybrid View

  1. #1
    Join Date
    Jun 2007
    Posts
    1

    Default What is difference between id and name in beans config xml?

    In beans configuration file:

    ------------------------------------------------------------------
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean id="xxx" name="xxx"
    ------------------------------------------------------------------

    what is the difference between id and name property except that id should be unique in the IOC container?

    Thanks.

    Spencers

  2. #2
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    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.

    (With the XSDs in Spring 2.0 the situation might be slightly different than it was with DTDs. They can express more complex validation rules, don't know if you can prevent duplicate name attributes in the whole file with them.)

    Jörg

  3. #3
    Join Date
    Mar 2005
    Location
    The Netherlands
    Posts
    121

    Default

    Quote Originally Posted by Jörg Heinicke View Post
    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.
    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.

  4. #4
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by Joris Kuipers View Post
    Also, you cannot use certain characters in an XML id attribute ...
    That's what I subsumed in

    Quote Originally Posted by Jörg Heinicke View Post
    The id gets automatically and stricter validated by the XML parser since it has a special meaning in XML.


    Jörg

  5. #5
    Join Date
    Jun 2012
    Location
    Rajsthan,India
    Posts
    2

    Default

    each bean have own unique id in container. id can not contain special character like '/' and patteren matching like '/*.html' etc.
    but name property can follow this feature best form spring mvc. name property can set with special character like "/*.html".which is directly called in Url also.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •