[JAVA] [Spring] The difference between a bean's naming problem (ID and name difference) and ref and IDREF in spring

Source: Internet
Author: User
Tags 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:

<bean 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:

<bean name= "Bean" class= "Com.zyh.spring3.hello.HelloWorld"/>  <alias alias= "alias1" name= "Bean"/>  

The difference between ref and IDREF

In spring, both the Idref property and the ref attribute can be injected into the constructor-arg element and the property element, so what's the difference between it?

Consider the following configuration:

<bean id= "Bea" class= "java.lang.String" ><constructor-arg index= "0" ><value>teststring</value ></constructor-arg></bean><bean id= "Beanid" class= "Com.zyh.spring3.hello.HelloWorld" >< Constructor-arg name= "name" ><idref bean= "Bea"/></constructor-arg><property name= "id" ><ref Local= "Bea"/></property><property name= "Age" value= "></property></bean>"

In fact, IDREF injects the ID of the target bean instead of the instance of the target bean, while the IDREF container verifies that the name's bean is actually present when deployed. In fact, IDREF is just like value, simply injecting a string into a property or constructor, but injecting the id attribute value of a bean definition. So in the code above

<constructor-arg name= "name" ><idref bean= "Bea"/></constructor-arg>

is actually equivalent to

<constructor-arg name= "Name" ><value>bea</value></constructor-arg>

While ref is completely different, the ref element injects an instance of the target bean definition into a property or constructor, and the ref element has three attributes, the difference is as follows:

1. Local can only specify the name of the bean defined in the same configuration file as the currently configured bean;
2. Parent can only specify object references defined in the parent container of the current container;
3, beans basically take all, that is, including the above two cases can be, so, take all cases, directly using the bean to specify the object reference can be.
So, in the above configuration code, the parameter name of the constructor in this bean is Beanid the string "Bea", and its id attribute is injected with the teststring string.

[JAVA] [Spring] The difference between a bean's naming problem (ID and name difference) and ref and IDREF in spring

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.