Spring Framework5.0 Learning (4)--bean named ID and name difference

Source: Internet
Author: User
Tags aliases throw exception

Name of Bean in spring

1, each bean can have an id attribute, and according to the ID in the IOC container to find the bean, the id attribute value must be unique in the IOC container;

2, you can not specify the id attribute, only specify the fully qualified class name, such as:

<class= "Com.zyh.spring3.hello.StaticBeanFactory"></Bean  >  

The bean needs to be obtained through interface Getbean (Class<t> requiredtype).

Throw exception if the bean cannot be found: nosuchbeandefinitionexception

Throw exceptions if there are more than one bean of that type: nouniquebeandefinitionexception

3, if you do not specify the ID, only specify name, then name is the bean identifier, and need to be unique in the container;

4. Specify both the name and the ID, at which point the ID is an identifier, and name is the Bean alias, both of which can find the target bean;

5, you can specify more than one name, can be separated by semicolons (";"), A Space ("") or a comma (","), if no ID is specified, then the first name is an identifier, the rest is an alias, if the id attribute is specified, the ID is an identifier, all names are aliases. Such as:

 <  bean  name  = "alias1 alias2;alias3,alias4"   ID  = "Hello1"   class  = "Com.zyh.spring3.hello.HelloWorld"   >  <  constructor-arg  index  = "0"   value  = "Rod"  ></ constructor-arg   >  </ bean  >  

At this point, Hello1 is an identifier, and Alias1,alias2,alias3,alias4 is an alias, which can be used as the bean's key value;

6, you can use the <alias> tag to specify aliases, aliases must also be unique in the IOC container, such as:

<name= "Bean"  class= "Com.zyh.spring3.hello.HelloWorld"/ >    <alias= "ALIAS1"  name= "Bean"  />    <alias= "Alias2"  name= "Bean"  />   

Spring Framework5.0 Learning (4)--bean named ID and name difference

Related Article

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.