Java Reference
In-Depth Information
Once we have decorated the specific instance we need to qualify, we can use our
qualifiers in the client code to specify the exact type of dependency we need.
package com.ensode.controller;
import com.ensode.model.Customer;
import com.ensode.model.PremiumCustomer;
import com.ensode.qualifier.Premium;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
@Named
@RequestScoped
public class PremiumCustomerController {
private static final Logger logger = Logger.getLogger(
PremiumCustomerController.class.getName());
@Inject
@Premium
private Customer customer;
public String saveCustomer() {
PremiumCustomer premiumCustomer = (PremiumCustomer) customer;
logger.log(Level.INFO, "Saving the following information \n"
+ "{0} {1}, discount code = {2}",
new Object[]{premiumCustomer.getFirstName(),
premiumCustomer.getLastName(),
premiumCustomer.getDiscountCode()});
//If this was a real application, we would have code to save
//customer data to the database here.
return "premium_customer_confirmation";
}
}
Since we used our @Premium qualifier to decorate the customer field, an instance
of PremiumCustomer is injected into that field, since this class is also decorated
with the @Premium qualifier.
As far as our JSF pages go, we simply access our named bean as usual using its
name.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
Search WWH ::




Custom Search