The difference between spring IDREF and ref

Source: Internet
Author: User
Tags xml parser

The IDREF element is used to pass the ID of the other bean in the container to the <constructor-arg/> or <property/> element, while providing error-validation functionality.

<bean id= "Thetargetbean" class= "..."/>
<bean id= "Theclientbean" class= "...". >
<property name= "TargetName" >
<idref bean= "Thetargetbean"/>
</property>
</bean>

The above bean definition fragment is completely equivalent to the following fragment (at run time)

<bean id= "Thetargetbean" class= "..."/>
<bean id= "Client" class= "..." >
<property name= "TargetName" value= "Thetargetbean"/>
</bean>

It's also the idref. I can get the value of the Bean's name (a string) in the spring container, not an instance of the bean.

The function of the IDREF element is similar to that of <value>, that is, the IDREF function is much more validated, which reduces the probability of writing errors in the configuration. In addition to the <idref bean= ""/>, if the referenced bean is in the same XML file, and the bean's name is the bean's ID, you can use the <IDFEF local= ""/> This property allows the XML parser to validate the referenced bean when parsing the XML.

In the value method, the TargetName property value passed to the client bean is not validated. Any input errors are found only when the client bean is actually instantiated (possibly with fatal errors).


And ref is the instance that gets the bean. Used to implement the injection function.

If just want to get the name of the bean using IDREF

Use the IDREF tag to allow the container to verify that the referenced bean exists at deployment time.



Ref has three properties: local, parent, Bean, as follows: Local can only specify the name of the object defined in the same configuration file as the currently configured object, and parent can only specify the object reference that is defined in the parent container of the current container; So, in all cases, it's OK to use the bean directly to specify the object reference.

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.