About setting the ref attribute in the spring configuration file

Source: Internet
Author: User
Tags xml parser

You can also use a REF element inside a <constructor-arg/> or <property/> element. This element is used to set the value of the specified property in the bean to a reference to another bean in the container. As mentioned earlier, the reference bean will be injected as a dependency and will be initialized before injection (if the singleton bean is already initialized by the container). Although it is a reference to another object, there are three different forms of pointing to another object through Id/name, and different forms determine how scopes and validations are handled.

The first and most common form is the target bean that specifies the Bean property by using the <ref/> tag, which can refer to any bean within the same container or parent container (whether or not in the same XML file). The value of the XML ' bean ' element can be either the ID value of the specified bean or its name value.

<ref bean= "Somebean"/>

The second form is to use the local property of ref to specify the target bean, which can use the XML parser to verify that the referenced bean exists in the same file. The local property value must be the ID property value of the target bean. If the referenced Bean,xml parser is not found in the same configuration file, an exception is thrown. If the target bean is within the same file, using local means is the best option (to find the error early).

<ref local= "Somebean"/>

The third Way is to refer to the bean in the parent container of the current container by using the Parent property of ref. The Parent property value can be either the ID value of the target bean or the value of the Name property. And the target bean must be in the parent container of the current container. The primary purpose of using the Parent property is to wrap a bean in the parent container with a proxy with the same name as the Bean in the parent container (for example, a bean definition in the child context overrides his parent bean).

<!--in the parent context--
<bean id= "Accountservice" class= "Com.foo.SimpleAccountService" >
    <!--insert dependencies as required as here--
</bean>
<!--in the (descendant) context--
<bean id= "Accountservice"  same as the name of the ' PA Rent ' bean
      class= "Org.springframework.aop.framework.ProxyFactoryBean" >
      <property name= "target" >
          <ref parent= "Accountservice"/>  parent Bean
      </property>
    <!--Insert other configuration and dependencies as required as --
</bean>

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.