Java Reference
In-Depth Information
To mark a bean as an alternative, annotate it with the javax.enterprise.inject.Altertative annotation, and then
specify it within an alternative element in the beans.xml file. For example, in this scenario, the human resources
department would like to implement a standard employee hiring process, as well as an alternative testing implementation.
All of the processes that must be initiated for hiring an employee are defined within an interface named Hire , as follows.
package org.javaee7.chapter07;
/**
* This is the definition of an employee hiring process. This interface
* declares the processes that must be initiated when an employee is hired.
* @author Juneau
*/
public interface Hire {
public String badgeEmployee();
public String trainEmployee();
}
The employee hiring implementation class must then implement Hire . In this case, both the standard class
and the testing class must implement the Hire interface, but each can have its own implementation of the declared
methods. For brevity, lets take a look at the test implementation, assuming that the standard implementation
looks exactly the same, but includes a different implementation for each of the methods. Note that the test
implementation class is annotated with @Alternative , meaning that it can be specified as an alternative for the
standard implementation class at deployment time. The following class, EmployeeProcessorTestImpl , is the test
implementation for the employee hiring process.
package org.javaee7.chapter07;
import javax.enterprise.inject.Alternative;
/**
* This bean would be a test implementation for the employee hiring process.
* To make this bean active, it must be specified within the beans.xml as an
* alternative implementation for EmployeeProcessor.
* @author Juneau
*/
@Alternative
public class EmployeeProcessorTestImpl implements Hire {
@Override
public String badgeEmployee() {
String result = null;
// Perform a test implementation for badging an employee
return result;
}
@Override
public String trainEmployee() {
String result = null;
// Perform a test implementation for training an employee
return result;
}
}
Search WWH ::




Custom Search