Java Reference
In-Depth Information
In the previous example, the
getBeanName
method will return the
String
-based name of the bean, and
getBeanScope
will return the
Annotation
class for the bean scope. The
Class<? extends Annotation>
is a generic
syntax that designates an
Annotation
type class. Table
7-1
lists the different methods available from the
Bean
interface
for obtaining metadata, along with a brief description of each.
Table 7-1.
Bean Interface Methods for Obtaining Metadata
Method
Returns
Description
getBeanClass
Class<?>
Returns the bean class of the managed or
session bean, or of the bean that declares the
producer method or field.
getInjectionPoints
Set<InjectionPoint>
Returns the InjectionPoint objects of the bean.
getName
String
Returns the EL name of the bean, if it exists.
Set<Annotation>
getQualifiers
Returns the bean's qualifiers.
getScope
Class<? extends Annotation>
Returns the scope of the bean.
getStereotypes
Set<Class ? extends Annotation>
Returns the stereotypes of the bean.
getTypes
Set<Type>
Returns the types of the bean.
isAlternative
boolean
Determines whether the bean is an alternative.
isNullable
boolean
Determines whether the
create()
method may
return a
null
value.
Injection of the ServletContext
It is now possible to inject
ServletContext
into application classes using the
javax.servlet.ServletContext
bean type. Doing so allows control of the servlet context for a particular application. As such, one can add listeners,
create filters, work with attributes, and so on, just as if a bean were a standard Java servlet class. To inject the
ServletContext
, write the following code:
@Inject
ServletContext context;
Let's take a look at how you can use the
ServletContext
information from a JSF application. Suppose that you
want to display the server information on a JSF view. In the following code for
org.javaee7.jsf.AcmeBean
, the
ServletContext
is injected using the new enhancement, and data from it is made available for use in a JSF view:
package org.javaee7.jsf;
...
import javax.servlet.ServletContext;
@ManagedBean(name = "acmeBean")
@SessionScoped
public class AcmeBean implements Serializable {
@EJB
private AcmeFacade facade;