Second approach: using Alternatives to solve the dependency at deployment point.   
With @Alternative annotation you can package multiple beans that have the same injection point without errors -- you can apply the @Alternative annotation to more than one bean and specify the bean you want to use in the CDI beans.xml configuration file.

So we need flagging both EJBs with the @Alternatives annotation:

    
@Alternative
public class SingletonBeanDB implements SingletonBean { .. }

    
@Alternative 
public class SingletonBeanMemory implements SingletonBean   { .. }

then, in the beans.xml file we will specify which implementation we will use for our EJBs. For example, supposing we want to use the SingletonBeanDB
    
 <alternatives>
   <class>com.packtpub.chapter4.ejb.SingletonBeanDB</class>
 </alternatives>


Within the PropertyManager we will just use a generic @Injection which will be resolved at deployment time by CDI:
    
public class PropertyManager implements Serializable {

 @Inject  
 SingletonBean ejb;  
 ......
}

The following class diagram resumes our class schema:

cdi using @Alternatives annotation

You can download the code here which includes also the JSF view for adding data to the cache.

 

 

You can run this example application on JBoss AS 6 / 7. The application server auto-detects that you are using CDI, once a WEB-INF/beans.xml file is found in the application.

0
0
0
s2smodern