/
Eager.java
175 lines (167 loc) · 6.92 KB
/
Eager.java
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
/*
* Copyright OmniFaces
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.omnifaces.cdi;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.RequestScoped;
import jakarta.enterprise.context.SessionScoped;
import jakarta.enterprise.util.Nonbinding;
import jakarta.faces.context.FacesContext;
import jakarta.faces.event.PhaseId;
import jakarta.faces.webapp.FacesServlet;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.omnifaces.cdi.eager.EagerBeansPhaseListener;
import org.omnifaces.cdi.eager.EagerBeansRepository;
import org.omnifaces.cdi.eager.EagerBeansWebListener;
import org.omnifaces.cdi.eager.EagerExtension;
/**
* <p>
* The CDI annotation <code>@</code>{@link Eager} specifies that a scoped bean is to be eagerly instantiated.
* <p>
* Faces native managed beans have been deprecated in favor of CDI managed beans. One feature that those Faces native
* managed beans had that's not directly available for CDI managed beans is the ability to eagerly instantiate
* application scoped beans.
* <p>
* OmniFaces fills this void and even goes one step further by introducing the <code>@Eager</code> annotation
* that can be applied to <code>@RequestScoped</code>, <code>@ViewScoped</code>,
* <code>@SessionScoped</code> and <code>@ApplicationScoped</code> beans. This causes these beans to be instantiated
* automatically at the start of each such scope instead of on demand when a bean is first referenced.
* <p>
* In case of <code>@RequestScoped</code> and <code>@ViewScoped</code> beans instantiation happens per request URI / view
* and an extra attribute is required for specifying this.
*
* <h2>Supported scopes</h2>
*
* <p>
* Currently supported scopes:
* <ol>
* <li> CDI {@link RequestScoped}
* <li> CDI {@link jakarta.faces.view.ViewScoped}
* <li> OmniFaces {@link ViewScoped}
* <li> CDI {@link SessionScoped}
* <li> CDI {@link ApplicationScoped}
* </ol>
*
* <h2>Usage</h2>
*
* <p>
* E.g.
* The following bean will be instantiated during application's startup:
* <pre>
* @Eager
* @ApplicationScoped
* public class MyEagerApplicationScopedBean {
*
* @PostConstruct
* public void init() {
* System.out.println("Application scoped init!");
* }
* }
* </pre>
* <p>
* <em>Note: you can also use the stereotype <code>@</code>{@link Startup} for this instead.</em>
* <p>
* The following bean will be instantiated whenever a session is created:
* <pre>
* @Eager
* @SessionScoped
* public class MyEagerSessionScopedBean implements Serializable {
*
* private static final long serialVersionUID = 1L;
*
* @PostConstruct
* public void init() {
* System.out.println("Session scoped init!");
* }
* }
* </pre>
* <p>
* The following bean will be instantiated whenever the URI <code>/components/cache</code> (relatively to the
* application root) is requested, i.e. when an app is deployed to <code>/myapp</code> at localhost this will correspond to
* a URL like <code>https://example.com/myapp/components/cache</code>:
* <pre>
* @Eager(requestURI = "/components/cache")
* @RequestScoped
* public class MyEagerRequestScopedBean {
*
* @PostConstruct
* public void init() {
* System.out.println("/components/cache requested");
* }
* }
* </pre>
*
* <h2><code>FacesContext</code> in <code>@PostConstruct</code></h2>
*
* <p>
* When using <code>@Eager</code> or <code>@Eager(requestURI)</code>, be aware that the {@link FacesContext} is
* <strong>not</strong> available in the <code>@PostConstruct</code>. Reason is, the {@link FacesServlet} isn't
* invoked yet at the moment <code>@Eager</code> bean is constructed. Only in <code>@Eager(viewId)</code>, the
* {@link FacesContext} is available in the <code>@PostConstruct</code>.
* <p>
* In case you need information from {@link HttpServletRequest}, {@link HttpSession} and/or {@link ServletContext}, then
* you can just <code>@Inject</code> it right away. Also, all other CDI managed beans are available the usual way
* via <code>@Inject</code>, as long as they do also not depend on {@link FacesContext} in their
* <code>@PostConstruct</code>.
*
* @since 1.8
* @author Arjan Tijms
* @see EagerExtension
* @see EagerBeansRepository
* @see EagerBeansPhaseListener
* @see EagerBeansWebListener
*
*/
@Documented
@Retention(RUNTIME)
@Target({ TYPE, METHOD })
public @interface Eager {
/**
* (Required when combined with {@link RequestScoped}) The URI (relative to the root of the web app) for which a
* request scoped bean should be instantiated. When this attribute is specified the bean will be instantiated very
* early during request processing, namely just before the first Servlet Filter is invoked, but after a SAM.
* <p>
* Faces services will not be available (yet) when the bean is instantiated this way.
* <p>
* If both this attribute and {@link Eager#viewId()} is specified, this attribute takes precedence for {@link RequestScoped}.
* This attribute <b>can not</b> be used for <code>ViewScoped</code> beans.
*
* @return The request URI relative to the context root.
*/
@Nonbinding
String requestURI() default "";
/**
* (Required when combined with {@link RequestScoped} or <code>ViewScoped</code>) The id of the view for which a request or view scoped bean
* should be instantiated. When this attribute is specified the bean will be instantiated during invocation of the
* {@link FacesServlet}, namely right after the RESTORE_VIEW phase (see {@link PhaseId#RESTORE_VIEW}).
*
* <p>
* Faces services are available when the bean is instantiated this way.
*
* <p>
* If both this attribute and {@link Eager#requestURI()} is specified and the scope is {@link RequestScoped}, the
* <code>requestURI</code> attribute takes precedence. If the scope is <code>ViewScoped</code> <code>requestURI</code> is ignored and only
* this attribute is considered.
*
* @return The view ID.
*/
@Nonbinding
String viewId() default "";
}