When to use and when to use in Spring?

前端 未结 5 1342
轻奢々
轻奢々 2020-12-14 06:51

When to use and when to use in Spring?

相关标签:
5条回答
  • 2020-12-14 06:58

    The local attribute on the ref element is no longer supported in the 4.0 beans xsd since it does not provide value over a regular bean reference anymore. Simply change your existing ref local references to ref bean when upgrading to the 4.0 schema.

    0 讨论(0)
  • 2020-12-14 07:01

    In spring 4.1 the local attribute is not valid.

    i used in the parent xml a name attribute for the

    and in the child file a referenced the bean by the alias i gave already.

    0 讨论(0)
  • 2020-12-14 07:11

    Specifying the target bean by using the bean attribute of the ref tag is the most general form, and will allow creating a reference to any bean in the same BeanFactory/ApplicationContext (whether or not in the same XML file), or parent BeanFactory/ApplicationContext. The value of the bean attribute may be the same as either the id attribute of the target bean, or one of the values in the name attribute of the target bean.

    <ref bean="someBean"/>
    

    Specifying the target bean by using the local attribute leverages the ability of the XML parser to validate XML id references within the same file. The value of the local attribute must be the same as the id attribute of the target bean. The XML parser will issue an error if no matching element is found in the same file. As such, using the local variant is the best choice (in order to know about errors are early as possible) if the target bean is in the same XML file.

    <ref local="someBean"/>
    

    This is from the Spring source reference here

    0 讨论(0)
  • 2020-12-14 07:20
    • <ref local="someBeanId"> should be used when you have a duplicate id in your parent-child config files and you want to distinguish between the two in either config file.

    • <ref parent="someBeanId"> should be used in the child config file to reference the parent id.

    • <ref bean="someBeanId"> should be used when you do not have a duplicate id in your parent-child config files.

    0 讨论(0)
  • 2020-12-14 07:25

    <ref local=".."> requires that the bean being referenced is in the same config file.

    <ref bean="..."> requires only it to be in the same context, or in a parent context.

    The difference is primarily one of documentation. If you see <ref local="...">, then you know you need only look in the same file to find it. Other than that, there's not much difference. I would generally use <ref bean="..."> in most cases.

    0 讨论(0)
提交回复
热议问题