Difference between the ID and name attributes in bean of applicationcontext. XML in spring

Source: Internet
Author: User
Tags xml parser

<Beans>
<Bean id = "person" class = "org. Spring. Bean. Person">
<Property name = "name">
<Value> zhangsan </value>
/Property>
<Property name = "Age">
<Value> 23 </value>
</Property>
</Bean>
</Beans>

Note: At the beginning of bean configuration, note that the attribute value of name in property must be the same as the name member variable in the bean class.

From the above. XML document, we can know that the ID attribute is a unique identifier of a bean. This ID must be a unique identifier in beanfactory or applicationcontext of the bean management. Usage:
Person P = (person) A. getbean ("person"); the parameter passed in the getbean () method is the bean ID attribute value.

Of course, we can also use the name attribute to specify the bean ID for demonstration.CodeAs follows:
<Beans>
<Bean name = "person, user" class = "org. Spring. Bean. Person">
<Property name = "name">
<Value> zhangsan </value>
</Property>
<Property name = "Age">
<Value> 23 </value>
</Property>
</Bean>
</Beans>

The difference between bean ID and name to specify bean ID:
The ID attribute allows us to specify the ID of a bean, and it is marked as the ID attribute of a real XML Element in the xml dtd, therefore, the XML Parser can perform some extra validation when other elements point to it. The name attribute is opposite to the ID attribute. If we need to use someInvalid characterYou can use the name attribute to specify one or more IDs. Use commas (,) or (;) to separate multiple IDs.

Usage: person P = (person) A. getbean ("person"); or rewrite it to person P = (person) A. getbean ("user ");

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.