Given the following interface:
public interface MyRunnable {
public MyResponse run(int x);
}
It is implemented by two @Stateless
beans:
@Stateless
public class Bean1 implements MyRunnable {
public MyResponse run(int x) {
// some logic
}
}
@Stateless
public class Bean2 implements MyRunnable {
public MyResponse run(int x) {
// some logic
}
}
Now, I want to run one bean or the other depending on a condition. What is the best way to achieve that?
This is what I tried, unsuccessfully:
@Stateless
@LocalBean
public class MainBean {
@Inject
private Bean1 bean1;
@Inject
private Bean2 bean2;
public void someMethod(int y) {
MyRunnable runnable = null;
if (y == 1)
runnable = bean1;
else
runnable = bean2;
runnable.run(5);
}
}
The code compiles, but it gives me (in Wildfly) a startup error:
WELD-001408: Unsatisfied dependencies for type Bean1 with qualifiers @Default
UPDATE
Something that I noticed; if I remove implements MyRunnable
from the beans there are no startup errors.