I am using code like below:
public Configuration {
private boolean isBatmanCar = someMethod(...);
@Produces
public Car getCar(@New Car car) {
Using @Alternative works but should only be used if you want to be able to activate it through beans.xml.
Suppressing the default constructor of your bean also works but you won't be able to use your bean in another scope than @RequestScoped.
Using your own qualifier works but isn't very useful if you have only one implementation and just want to be able to instantiate your bean with a producer rather than with its constructor.
The easiest way is to annotate your bean @Any :
@Any
public class Car {
}
...
@Produces
public Car getCar() {
return new Car();
}
...
@Inject
Car car;
Things that you have to keep in mind :
Regarding all this, the same code as above explicitly qualified looks like this :
@Any
public class Car {
}
...
@Produces
@Any
@Default
public Car getCar() {
return new Car();
}
...
@Inject
@Default
Car car;
It becomes more obvious that the bean's default constructor isn't a valid possibility for the injection point and the producer is a valid possibility.