<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 ");