-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaultbeans.xml
executable file
·82 lines (64 loc) · 3.08 KB
/
defaultbeans.xml
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<chapter id="defaultbeans">
<title>Default Beans</title>
<para>
Suppose you have a situation where you want to provide a default implementation of a particular service and allow
the user to override it as needed. Although this may sound like a job for an alternative, they have some
restrictions that may make them undesirable in this situation. If you were to use an alternative it would require
an entry in every <code>beans.xml</code> file in an application.
</para>
<para>
Developers consuming the extension will have to open up the any jar file which references the default bean, and
edit the <code>beans.xml</code> file within, in order to override the service. This is where default beans come
in.
</para>
<para>
Default beans allow you to create a default bean with a specified type and set of qualifiers. If no other bean is
installed that has the same type and qualifiers, then the default bean will be installed.
</para>
<para>
Let's take a real world example - a module that allows you to evaluate EL (something that Seam Solder
provides!). If JSF is available we want to use the <code>FunctionMapper</code> provided by the JSF implementation
to resolve functions, otherwise we just want to use a a default <code>FunctionMapper</code> implementation that
does nothing. We can achieve this as follows:
</para>
<programlisting role="JAVA">@DefaultBean(type = FunctionMapper.class)
@Mapper
class FunctionMapperImpl extends FunctionMapper {
@Override
Method resolveFunction(String prefix, String localName) {
return null;
}
}</programlisting>
<para>
And in the JSF module:
</para>
<programlisting role="JAVA">class FunctionMapperProvider {
@Produces
@Mapper
FunctionMapper produceFunctionMapper() {
return FacesContext.getCurrentInstance().getELContext().getFunctionMapper();
}
}</programlisting>
<para>
If <code>FunctionMapperProvider</code> is present then it will be used by default, otherwise the default
<code>FunctionMapperImpl</code> is used.
</para>
<para>
A producer method or producer field may be defined to be a default producer by placing the
<code>@DefaultBean</code> annotation on the producer. For example:
</para>
<programlisting role="JAVA">class CacheManager {<abbrev>
@DefaultBean(Cache.class)
Cache getCache() {
...
}
}</abbrev></programlisting>
<para>
Any producer methods or producer fields declared on a default managed bean are automatically registered as default
producers, with <code>Method.getGenericReturnType()</code> or <code>Field.getGenericType()</code> determining the
type of the default producer. The default producer type can be overridden by specifying <code>@DefaultBean</code>
on the producer method or field.
</para>
</chapter>