i have following interface:
public interface resultevaluationinterface { public void evaluateresults(event e); } and want inject in class depending on event.type different classes same implementation. that:
@stateless @localbean public class resultevaluation implements resultevaluationinterface { @override public void evaluateresults(event e) { switch (e.gettype()) { case type.running: // inject , call resultevaluationrunningejb.evaluateresults(e) case type.swimming: // inject , call resultevaluationswimmingejb.evaluateresults(e) default: throw new unsupportedoperationexception("not supported yet."); } } } resultevaluationrunningejb , resultevaluationswimmingejb both implement interface. has got idea how in way?
if want use hard coded if statement switch between prod , dev events use cdi qualifiers inject 2 implementations facade:
@stateless @localbean public class resultevaluationfacade { @inject @development private resultevalutationinterface dev; @inject @production private resultevalutioninterface prod; @override public void evaluateresults(event e) { switch (e.gettype()) { case type.production: prod.evaluteresult(e); break; case type.development: dev.evaluteresult(e); break; default: throw new unsupportedoperationexception("not supported yet."); } } } and define 2 implementations:
@development public class resultevaluationdevelopment implements resultevaluationinterface { ... } @production public class resultevaluationdevelopment implements resultevaluationinterface { ... } however consider using mock maven project house 2 separate implementations instead.
alternatively use different cdi event types, this.
public void observedevevent(@observe devevent event) { //do stuff. } public void observeprodevent(@observe prodevent event) { //do stuff } firing event this:
@inject private event<prodevent> prodevent; public void somemethod() { prodevent pe = new prodevent() // set data on prodevent prodevent.fire(pe); } note events can work qualifiers, add qualifier annotation event instead of implementing 2 different types of event.
@inject @production private event<myevent> event; and listen @prodcution events;
public void handleprodevent(@observer @production myevent myevent) { // stuff. } for lazy instantiation of beans can use cdi instance injection.
@inject private instance<beana> beana; .... public void dostuff(event e) { ... case type.production: //lazily evaluates , instantiatiates bean. beana.get().evaluateresult(e); }
Comments
Post a Comment