The difference between the spring Bean ID and the bean name

Source: Internet
Author: User

Original: http://selppay-gmail-com.iteye.com/blog/763263

1. Spring Bean defination

In general, we define and apply beans using bean IDs, but today I have this definition

XML code
  1. <bean id = "writeservice" class = "Aserviceimp, Bserviceimp, cserviceimp"
  2. name = "Aservice, bservice, cservice" Init-method = "init">
  3. <property name = "InterfaceName">
  4. <value>baseservice></value>
  5. </Property>
  6. ............
  7. </Bean>

Such a definition first indicates aservice, bservice, Cservice implements interface Baseservice

When injected with the spring IOC, you can use either the ID writeservice or the name Aservice/bservice/cservice

What is the difference between Bean ID and bean name?

Google and Baidu can not find the answer I want, and finally in spring's official web found the following explanation, I understand that: the definition of bean name is legal, but a bean name can not be unique to represent a bean, That is, multiple beans can have the same bean name, but the bean ID is the only one that identifies a bean, so we should try to avoid using the bean name to refer to the bean.

Original explanation:

For a fallback match, the bean name is considered as a default qualifier value. This means, the bean is defined with an ID "main" instead of the nested qualifier element, leading to the same mat Ching result. However, note that and this can used to refer to specific beans by name, @Autowired was fundamentally about TYPE-DRIV En injection with optional semantic qualifiers. This means so qualifier values, even when using the beans name fallback, always has narrowing semantics within the set O F type matches; They does not semantically express a reference to a unique bean ID. Good qualifier values would be "main" or "EMEA" or "persistent", expressing characteristics of a specific component-inde Pendent from the bean ID (which is auto-generated in case of a anonymous bean definition like the one above).

The difference between the spring Bean ID and the bean name

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.