Difference between using bean id and name in Spring configuration file

后端 未结 6 1135
忘了有多久
忘了有多久 2020-12-04 05:24

Is there any difference between using an id attribute and name attribute on a element in a Spring configuration file?

6条回答
  •  隐瞒了意图╮
    2020-12-04 06:25

    Since Spring 3.1 the id attribute is an xsd:string and permits the same range of characters as the name attribute.

    The only difference between an id and a name is that a name can contain multiple aliases separated by a comma, semicolon or whitespace, whereas an id must be a single value.

    From the Spring 3.2 documentation:

    In XML-based configuration metadata, you use the id and/or name attributes to specify the bean identifier(s). The id attribute allows you to specify exactly one id. Conventionally these names are alphanumeric ('myBean', 'fooService', etc), but may special characters as well. If you want to introduce other aliases to the bean, you can also specify them in the name attribute, separated by a comma (,), semicolon (;), or white space. As a historical note, in versions prior to Spring 3.1, the id attribute was typed as an xsd:ID, which constrained possible characters. As of 3.1, it is now xsd:string. Note that bean id uniqueness is still enforced by the container, though no longer by XML parsers.

提交回复
热议问题