//enterprise java /
@RequestScoped and @Named annotations. On each request, a new instance
of the EISConnector is injected to the
Index managed bean. A stateless session bean would cause only a single
injection per pooled instance.
LISTING 1 LISTING 2 LISTING 3 LISTING 4 LISTING 5 LISTING 6
COMMUNITY
Configuration with Convention
In addition to stages, any arbitrary
primitive (String, int, long, float) and
other objects can be configured dynamically. Let’s assume that an imaginary
customer expects us to build a highly
configurable Greeter component (see
Listing 6).
The Greeter stateless session bean
returns a configurable message that is
concatenated a configurable number of
times. We simply use the @Inject annotation to inject primitive members, as
shown in Listing 6.
The injection of the Greeter#message
and Greeter#repetition attributes immediately causes the deployment error
shown in Listing 7.
The “unsatisfied dependencies” error
indicates the lack of adequate instances
for the injection. We have to make the
primitive available for injection first by
using the @Produces annotation. The
easiest possible solution for the “
unsatisfied dependencies” problem is direct
exposure of attributes with matching
types in a dedicated class:
@Model
public class FacesContextExposer {
@Produces
public Stage stage(){
FacesContext ctx = FacesContext.getCurrentInstance();
return Stage.valueOf( ctx.getApplication().getProjectStage().name());
}
}
JAVA IN ACTION
Download all listings in this issue as text
ABOUT US
A dependency
between the
configuration and the
fully qualified class
names can cause
inconsistencies.
public class Configurator{
private String msg =
"configurable";
private int repetition = 2;
}
Although the introduction of the
Configurator class fixes the problem for
our simplistic demands and centralizes
all configuration data in one place, this
approach is not usable in real projects.
You could configure only a single String
that way. The Contexts and Dependency
Injection (CDI) runtime matches
the attribute types, not the names.
Regardless of the attribute’s name, all
Strings would get the same value pro-
duced in the Configurator.
blog
60
ORACLE.COM/JAVAMAGAZINE /////////////////////////////////////////////// MAY/JUNE 2012