Bean naming in spring and the difference between ref and idref

Source: Internet
Author: User

I have been using spring for a long time, but I know little about it. I just learned about Bean naming in spring and the differences between ref and idref. I will make a slight record for future reference.

Bean naming in spring

1. Each bean can have an id attribute and search for the bean in the IOC container Based on the ID. The ID attribute value must be unique in the IOC container;

2. You can specify only the fully qualified class name without specifying the ID attribute, for example:

<bean class="com.zyh.spring3.hello.StaticBeanFactory"></bean>

In this case, you need to use the getbean interface (class <t> requiredtype) to obtain the bean;

3. If no ID is specified and only name is specified, name is the bean identifier and must be unique in the container;

4. specify both name and ID. At this time, ID is the identifier and name is the bean alias. Both of them can find the target bean;

5. Multiple names can be specified, which can be separated by semicolons (";"), spaces (""), or commas (","). If no ID is specified, the first name is the identifier, and the rest is the alias. If the ID attribute is specified, the ID is the identifier, and all names are aliases. For example:

<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 time, hello1 is the identifier, while alias1, alias2, alias3, and alias4 are aliases. They can all be used as bean key values;

6. You can use the <alias> label to specify an alias. the alias must also be unique in the IOC container, for example:

<bean name="bean" class="com.zyh.spring3.hello.HelloWorld"/>  <alias alias="alias1" name="bean"/>  <alias alias="alias2" name="bean"/> 

Differences between ref and idref

In spring, the idref and ref attributes can be used in the constructor-Arg element and the property element for injection. What is the difference between them?

Consider the following configuration code:

<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="25"></property></bean>

In fact, idref injects the ID of the target bean instead of the target bean instance, and uses the idref container to verify whether the bean with this name actually exists during deployment. In fact, idref is the same as value. It only injects a string into the attribute or constructor, but injects the ID attribute value defined by a bean. So in the above Code

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

Actually equivalent

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

The ref element is completely different. The ref element injects the instance defined by the target bean into the attribute or constructor. The ref element has three attributes. The differences are as follows:

1. Local can only specify the name defined by bean in the same configuration file as the bean currently configured;
2. parent can only specify object references defined in the parent container of the current container;
3. Bean can be used in both cases. Therefore, you can use bean to specify object references.
Therefore, in the preceding configuration code, the constructor name in beanid bean injects only the string "Bea", while the ID property injects the string "teststring.

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.