I am using code like below:
public Configuration {
private boolean isBatmanCar = someMethod(...);
@Produces
public Car getCar(@New Car car) {
if(isBatmanCar) {
car.setName("BatmanCar");
}
return car;
}
}
public Car {
private String name = "NormalCar";
public void setName(String name) {
this.name = name;
}
}
public Demo {
@Inject
Car car;
// rest of code
}
When I deploy an application to glassfish (Java EE 6 btw) I get
AmbiguousResolutionException: WELD-001318 Cannot resolve an ambiguous dependency between (...) Car with qualifiers [@Any @Default] (...) Producer Method [Car] with qualifiers [@Any @Default]
I know that when I add @Alternative
to Car class it will work, but I wonder if this is the proper way to do it, and why do I have to do it?
Can you tell me what is the correct usage of @Produces in such case?
I'm using Java EE 6, CDI 1.0, EJB 3.1, Glassfish 3.2