This repository was archived by the owner on Jan 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathcdi-basic014.html
More file actions
221 lines (209 loc) · 6.97 KB
/
cdi-basic014.html
File metadata and controls
221 lines (209 loc) · 6.97 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Using the @PostConstruct and @PreDestroy Annotations with CDI Managed Bean Classes</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/style.css" rel="stylesheet">
<script src="https://use.fontawesome.com/96c4d89611.js"></script>
</head>
<body>
<table id="doc-title" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top">
<b>Java Platform, Enterprise Edition (Java EE) 8</b><br />
<b>The Java EE Tutorial</b>
<!-- <p class="beta">Beta Draft (Pre-General Availability)</p> -->
</td>
</tr>
</table>
<hr />
<table width="90%" id="top-nav" cellspacing="0" cellpadding="0">
<colgroup>
<col width="12%"/>
<col width="12%"/>
<col width="*"/>
</colgroup>
<tr>
<td align="left">
<a href="cdi-basic013.html">
<span class="vector-font"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Previous</span>
</a>
</td>
<td align="left">
<a href="cdi-basic015.html">
<span class=" vector-font"><i class="fa fa-arrow-circle-right vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Next</span>
</a>
</td>
<td align="right">
<a href="toc.html">
<span class=" vector-font"><i class="fa fa-list vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Contents</span>
</a>
</td>
</tr>
</table>
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p><a id="BABJFEAI"></a><a id="using-the-postconstruct-and-predestroy-annotations-with-cdi-managed-bean-classes"></a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_using_the_postconstruct_and_predestroy_annotations_with_cdi_managed_bean_classes">Using the @PostConstruct and @PreDestroy Annotations with CDI Managed Bean Classes</h2>
<div class="sectionbody">
<div class="paragraph">
<p>CDI managed bean classes and their superclasses support the annotations
for initializing and for preparing for the destruction of a bean. These
annotations are defined in JSR 250: Common Annotations for the Java
platform (<code><a href="http://jcp.org/en/jsr/detail?id=250" class="bare">http://jcp.org/en/jsr/detail?id=250</a></code>).</p>
</div>
<div class="paragraph">
<p>The following topics are addressed here:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#CIHEHHCH">To Initialize a Managed Bean Using the @PostConstruct
Annotation</a></p>
</li>
<li>
<p><a href="#CIHBAFAC">To Prepare for the Destruction of a Managed Bean Using
the @PreDestroy Annotation</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p><a id="CIHEHHCH"></a><a id="to-initialize-a-managed-bean-using-the-postconstruct-annotation"></a></p>
</div>
<div class="sect2">
<h3 id="_to_initialize_a_managed_bean_using_the_postconstruct_annotation">To Initialize a Managed Bean Using the @PostConstruct Annotation</h3>
<div class="paragraph">
<p>Initializing a managed bean specifies the lifecycle callback method that
the CDI framework should call after dependency injection but before the
class is put into service.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>In the managed bean class or any of its superclasses, define a
method that performs the initialization that you require.</p>
</li>
<li>
<p>Annotate the declaration of the method with the
<code>javax.annotation.PostConstruct</code> annotation.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When the managed bean is injected into a component, CDI calls the method
after all injection has occurred and after all initializers have been
called.</p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 100%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p>Note:</p>
</div>
<div class="paragraph">
<p>As mandated in JSR 250, if the annotated method is declared in a
superclass, the method is called unless a subclass of the declaring
class overrides the method.</p>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>The <code>UserNumberBean</code> managed bean in
<a href="cdi-basicexamples003.html#GJCXV">The guessnumber-cdi CDI Example</a>
uses <code>@PostConstruct</code> to annotate a method that resets all bean fields:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@PostConstruct
public void reset () {
this.minimum = 0;
this.userNumber = 0;
this.remainingGuesses = 0;
this.maximum = maxNumber;
this.number = randomInt.get();
}</code></pre>
</div>
</div>
<div class="paragraph">
<p><a id="CIHBAFAC"></a><a id="to-prepare-for-the-destruction-of-a-managed-bean-using-the-predestroy-annotation"></a></p>
</div>
</div>
<div class="sect2">
<h3 id="_to_prepare_for_the_destruction_of_a_managed_bean_using_the_predestroy_annotation">To Prepare for the Destruction of a Managed Bean Using the @PreDestroy Annotation</h3>
<div class="paragraph">
<p>Preparing for the destruction of a managed bean specifies the lifecycle
call back method that signals that an application component is about to
be destroyed by the container.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>In the managed bean class or any of its superclasses, prepare for
the destruction of the managed bean.</p>
<div class="paragraph">
<p>In this method, perform any cleanup that is required before the bean is
destroyed, such as releasing a resource that the bean has been holding.</p>
</div>
</li>
<li>
<p>Annotate the declaration of the method with the
<code>javax.annotation.PreDestroy</code> annotation.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>CDI calls this method before starting to destroy the bean.</p>
</div>
</div>
</div>
</div>
<hr />
<table width="90%" id="bottom-nav" cellspacing="0" cellpadding="0">
<colgroup>
<col width="12%"/>
<col width="12%"/>
<col width="*"/>
</colgroup>
<tr>
<td align="left">
<a href="cdi-basic013.html">
<span class=" vector-font"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Previous</span>
</a>
</td>
<td align="left">
<a href="cdi-basic015.html">
<span class="vector-font"><i class="fa fa-arrow-circle-right vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Next</span>
</a>
</td>
<td align="right">
<a href="toc.html">
<span class="vector-font"><i class="fa fa-list vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Contents</span>
</a>
</td>
</tr>
</table>
<span id="copyright">
<a href="img/cpyr.adoc">
<img src="img/oracle.gif" height="10px" alt="Oracle Logo" />
<span>Copyright © 2017, Oracle and/or its affiliates. All rights reserved.</span>
</a>
</span>
<!--<p align="center" class="beta">Beta Draft (Pre-General Availability)</p> -->
</body>
</html>