The distinction between the ID and name of <bean> in spring

Source: Internet
Author: User

1. ID and name attribute are the same, the recommended use ID;

2. The ID value requirement is strict, must satisfy the XML naming specification. The ID is unique, and no two <bean> of the same ID is allowed in the configuration file.

3. The name value is more casual and can even start with a number. Two <bean> with the same name are allowed in the configuration file, and the subsequent bean is returned when the instance is returned with Getbean ().

4. If there is no id,name, use the full name of the class as name, such as <bean class= "test. Test, you can use Getbean ("test"). Test ") returns the instance.

5. ID when separated by a semicolon (";"), A Space ("") or a comma (",") can only be used as an identity, name with a semicolon (";"), A Space ("") or a comma (",")

Separate the multiple identities that will be part of the component (equivalent to the role of alias aliases).

Such as:

Name= "1 2 3 4" equivalent to Name= "1,2,3,4" is equivalent to having 1 2 3 4 (4 ) identifiers identifying the current bean

Id= "1 2 3 4" This is equivalent to having a "1 2 3 4" (1 ) identifier identifying the current bean

6. If the ID is configured and name is configured, then two are valid.

7. If there are multiple IDs and the name is not specified, and the instance class is the same, such as:

Code

<bean class= "Com.stamen.BeanLifeCycleImpl"/><bean class= "Com.stamen.BeanLifeCycleImpl"/><bean class= "Com.stamen.BeanLifeCycleImpl"/>

The first bean is obtained through Getbean ("Com.stamen.BeanLifeCycleImpl"),

The second bean is obtained through Getbean ("com.stamen.beanlifecycleimpl#1"),

The third bean is obtained through Getbean ("com.stamen.beanlifecycleimpl#2"), and so on.

8. When annotations and configuration files are present

If the annotations and configuration files are used when the basic class is configured, the annotations and the configuration file are different when the name is not the same, then the two does not conflict and can take effect.

If the annotations and configuration files are used when configuring the base class, the annotations and the configuration file name are the same when the two conflicts, the configuration file takes effect.

If the class is referenced as a reference class and is automatically injected, the annotations and configuration files are configured, and if the name is the same, the configuration file takes effect.

If the class is referred to as a reference class and is automatically injected when annotations and configuration files are configured, if the name is not the same, match the autowired match rule.

Note: Matching rules for autowired 1.ByType 2. ByName

Reference:

Http://blog.csdn.net/qq_22063697/article/details/51912386

Http://www.cnblogs.com/flying607/p/5132293.html

The distinction between the ID and name of <bean> in spring

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.