Repository navigation
Expand file tree
/
Copy pathUseLocalRef.qhelp
More file actions
58 lines (45 loc) · 2.41 KB
/
Copy pathUseLocalRef.qhelp
File metadata and controls
58 lines (45 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
If at all possible, refer to Spring beans in the same XML file using local references, that is <code><idref local="targetBean"></code>. This
requires that the bean being referenced is in the same XML file, and is named using the <code>id</code> attribute. Using local references
has the advantage of allowing reference errors to be detected during XML parsing, instead of during deployment or instantiation.
</p>
<p>
From the Spring Framework Reference documentation on <code>idref</code> elements:</p>
<blockquote>
<p>
[Using the <code>idref</code> tag in a <code>property</code> element] is preferable to [using the bean name in the property's <code>value</code> attribute], because using the <code>idref</code>
tag allows the container to validate at deployment time that the referenced, named bean actually exists. In the second variation, no validation
is performed on the value that is passed to the [<code>name</code>] property of the client bean. Typos are only discovered (with most likely fatal results)
when the client bean is actually instantiated. If the client bean is a prototype bean, this typo and the resulting exception may only be
discovered long after the container is deployed.
</p>
<p>
Additionally, if the referenced bean is in the same XML unit, and the bean name is the bean <code>id</code>, you can use the <code>local</code> attribute, which allows
the XML parser itself to validate the bean <code>id</code> earlier, at XML document parse time.
</p>
</blockquote>
</overview>
<recommendation>
<p>
Use a local <code>idref</code> when referring to beans in the same XML file. This allows errors to be detected earlier, at XML parse time
rather than during instantiation.
</p>
</recommendation>
<example>
<p>In the following example, the <code>shippingService</code> bean is shown using the <code>ref</code> element,
which cannot be checked by the XML parser. The <code>orderService</code> bean is shown using the
<code>idref</code> element, which allows the XML parser to find any errors at parse time.</p>
<sample src="UseLocalRef.xml" />
</example>
<references>
<li>
Spring Framework Reference Documentation 3.0:
<a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-value-element">3.4.2.1 Straight values (primitives, Strings, and so on)</a>.
</li>
</references>
</qhelp>