0
votes

I have a collection of Processor beans in my application along with a factory for creating them.

public abstract class Processor {
    public Processor(String config) { .... }
    public abstract void process() throws Exception;
}

public class Processor1 extends Processor {
    public Processor1(String config) { super(config);..}
    public void process() {....}
}

public Processor newProcessor(String impl, String config) {
    // use reflection to create processor
}

Can I use CDI to replace the factory class/method? And instead use a @Produces?

I tried using the following to iterate or select the instance I wanted. But Weld tells me that allProcessorInstances.isUnsatisfied() == true. I had to create default no-args ctor in order for Weld to find my Processor subclasses.

@Inject @Any Instance<Processor> allProcessorInstances;

Is there any way to tell the CDI container to use the constructor I want it to use? Or am I thinking about this problem the wrong way?

1

1 Answers

2
votes

To use the constructor you'd need to annotate it with @Inject, however, every param on the constructor must itself be a bean or something in the CDI scopes.

Using a producer method and having that take an InjectionPoint as a param, then having your configuration be part of an annotation would work.