.
- ?
@Alternatives. - ? , . .
- , ? , , . , /
@PostConstruct. . . 2. . ? , , 1.. @Calculator Calculator . , . 2.
@ApplicationScoped
public class CalculatorFctory implements Serializable {
private Calculator calc;
@Produces @Calculator Calculator getCalculator() {
return new Calculator();
}
}
Update:
CDI . , , , , , . , .
:
public class ImplOne implements MyInterface {
...
}
public class ImplTwo implements MyInterface {
...
}
, - :
@Inject ImplOne bean;
@Inject ImplTwo bean;
, @Calculator Calculator . , , . , @QualOne @QualTwo:
@Inject @QualOne ImplOne bean;
@Inject @QualTwo ImplTwo bean;
, .
, , :
@Inject @QualOne MyInterface bean;
@Inject @QualTwo MyInterface bean;
OP @Produces, , CBC CDI.
@Avinash Singh - CDI @Produces, , , CDI, . . , . `@... Scoped beans, , ..
, :
public class SomeProducer {
@Inject ImplOne implOne;
@Inject ImplTwo implTwo;
@Inject ImplThree implThree;
@Produces
public MyInterface get() {
if (conditionOne()) {
return implOne;
} else if (conditionTwo()) {
return implTwo;
} else {
return implThree;
}
}
}
public class SomeProducer {
@Produces
public MyInterface get() {
if (conditionOne()) {
return new ImplOne();
} else if (conditionTwo()) {
return new ImplTwo();
} else {
return new ImplThree;
}
}
}
CDI (.. @PostConstruct @Inject ) , , .
- ? , , .
@Default
public class ImplOne implements MyInterface {
...
}
@Alternative
public class ImplTwo implements MyInterface {
...
}
@Alternative
public class ImplThree implements MyInterface {
...
}
@Inject MyInterface instance, ImplOne ,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<alternatives>
<class>ImplTwo</class>
</alternatives>
</beans>
ImplTwo .
Java EE , CDI, EJB -.
- CDI bean? :
@WebServiceRef(lookup="java:app/service/PaymentService")
PaymentService paymentService;
, , CDI.
, @WebServiceRef(lookup="java:app/service/PaymentService") , ? , ? -:
@Produces @WebServiceRef(lookup="java:app/service/PaymentService")
PaymentService paymentService;
CDI bean, , @Inject CDI :
@Inject PaymentService paymentService;
, PaymentService CDI. -. , - CDI, - CDI.