Annotation Type Eager
-
@Documented @Retention(RUNTIME) @Target({TYPE,METHOD}) public @interface Eager
The CDI annotation
@
Eager
specifies that a scoped bean is to be eagerly instantiated.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.
OmniFaces fills this void and even goes one step further by introducing the
@Eager
annotation that can be applied to@RequestScoped
,@ViewScoped
,@SessionScoped
and@ApplicationScoped
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.In case of
@RequestScoped
and@ViewScoped
beans instantiation happens per request URI / view and an extra attribute is required for specifying this.Supported scopes
Currently supported scopes:
- CDI
RequestScoped
- CDI
ViewScoped
- OmniFaces
ViewScoped
- CDI
SessionScoped
- CDI
ApplicationScoped
Usage
E.g. The following bean will be instantiated during application's startup:
@Eager @ApplicationScoped public class MyEagerApplicationScopedBean { @PostConstruct public void init() { System.out.println("Application scoped init!"); } }
Note: you can also use the stereotype
@
Startup
for this instead.The following bean will be instantiated whenever a session is created:
@Eager @SessionScoped public class MyEagerSessionScopedBean implements Serializable { private static final long serialVersionUID = 1L; @PostConstruct public void init() { System.out.println("Session scoped init!"); } }
The following bean will be instantiated whenever the URI
/components/cache
(relatively to the application root) is requested, i.e. when an app is deployed to/myapp
at localhost this will correspond to a URL likehttps://example.com/myapp/components/cache
:@Eager(requestURI = "/components/cache") @RequestScoped public class MyEagerRequestScopedBean { @PostConstruct public void init() { System.out.println("/components/cache requested"); } }
FacesContext
in@PostConstruct
When using
@Eager
or@Eager(requestURI)
, be aware that theFacesContext
is not available in the@PostConstruct
. Reason is, theFacesServlet
isn't invoked yet at the moment@Eager
bean is constructed. Only in@Eager(viewId)
, theFacesContext
is available in the@PostConstruct
.In case you need information from
HttpServletRequest
,HttpSession
and/orServletContext
, then you can just@Inject
it right away. Also, all other CDI managed beans are available the usual way via@Inject
, as long as they do also not depend onFacesContext
in their@PostConstruct
.- Since:
- 1.8
- Author:
- Arjan Tijms
- See Also:
EagerExtension
,EagerBeansRepository
,EagerBeansPhaseListener
,EagerBeansWebListener
- CDI
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description String
requestURI
(Required when combined withRequestScoped
) The URI (relative to the root of the web app) for which a request scoped bean should be instantiated.String
viewId
(Required when combined withRequestScoped
orViewScoped
) The id of the view for which a request or view scoped bean should be instantiated.
-
-
-
Element Detail
-
requestURI
String requestURI
(Required when combined withRequestScoped
) 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.Faces services will not be available (yet) when the bean is instantiated this way.
If both this attribute and
viewId()
is specified, this attribute takes precedence forRequestScoped
. This attribute can not be used forViewScoped
beans.- Returns:
- The request URI relative to the context root.
- Default:
- ""
-
-
-
viewId
String viewId
(Required when combined withRequestScoped
orViewScoped
) 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 theFacesServlet
, namely right after the RESTORE_VIEW phase (seePhaseId.RESTORE_VIEW
).Faces services are available when the bean is instantiated this way.
If both this attribute and
requestURI()
is specified and the scope isRequestScoped
, therequestURI
attribute takes precedence. If the scope isViewScoped
requestURI
is ignored and only this attribute is considered.- Returns:
- The view ID.
- Default:
- ""
-
-