Outline
Through a simple CDI Web application demos dependency injection, scope, qualifiers and EL integration. After the completion of the deployment of applications we can http: 8080 / moodchecker: // localhost to access the Web application welcome page, Mood constantly change between good and bad. In this article, we can understand how qualifiers influence the selection of an injector .
Compile deploy test applications
The present application source code is https://github.com/kylinsoong/webframework/tree/master/cdi/moodchecker .We can use the software installation and data downloading method described download, compile, generate a deployment package cdi-moodchecker.war, use use 4 ways to deploy applications to JBoss7 / WildFly method described in the deployment cdi-moodchecker.war to JBoss node. Start JBoss 7 application deployment is completed as follows:
./standalone.sh
After the completion of the deployment of applications we can http: 8080 / moodchecker: // localhost access, Web page shown below:
Examples of analysis
Example relates to the profile or java class comprises:
- beans.xml
- faces-config.xml
- index.xhtml
- web.xml
- jboss-web.xml
- org.jboss.demo.cdi.moodchecker.Mood
- org.jboss.demo.cdi.moodchecker.BadMood
- org.jboss.demo.cdi.moodchecker.GoodMood
- org.jboss.demo.cdi.moodchecker.MoodChecker
Mood
Mood categories as follows:
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
@Named
@RequestScoped
public class Mood {
private String mood;
@Inject
@Good
private MoodChecker moodChecker;
public Mood() {
}
public void check() {
mood = moodChecker.checkMood();
}
public String getMood() {
return mood;
}
}
Note: @Named and @RequestScoped label class itself, MoodChecker properties and use @Inject @Good injection.
Qualifiers
Note that in front of Good is our custom label for injecting GoodMood, we have created here two tagging, Good and Bad, as follows:
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Qualifier
@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
public @interface Good {
}
We use the Good mark GoodMood, as follows:
@Good
public class GoodMood implements MoodChecker {
public String checkMood() {
return "I feel great !";
}
}
Thus when using @Good labels will Mood injection GoodMood.
EL integration
Mood index.xhtml use EL expressions to interact directly with back-Bean, index.xhtml reads as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>How Are You Today?</title>
</h:head>
<h:body>
<h3>How Are You?</h3>
<h:form>
<p>
<h:commandButton value="Check Mood" action="#{mood.check}" />
</p>
<p>
<h:outputText value="#{mood.mood}" />
</p>
</h:form>
</h:body>
</html>
Reproduced in: https: //my.oschina.net/iwuyang/blog/197250