Reputation: 19
I have this abstract class
public abstract class GenericScheduleController implements Serializable {
@Inject
private Service service;
@PostConstruct
private void init() {
service.doSomething(getLabel());
}
protected abstract String getLabel();
}
and I would like programmatically inject a new one dynamically.
public <T extends GenericScheduleController> T getScheduleController(String chaine) {
//TODO
//get new CDI instance programmatically with abstract getLabel() return chaine
}
Is it possible ?
Thx
Upvotes: 0
Views: 199
Reputation: 19
I do this and it's ok
public abstract class GenericScheduleController implements Serializable {
@Inject
private Service service;
@PostConstruct
private void init() {
if (getLabel() != null) {
service.doSomething(getLabel());
}
}
protected abstract String getLabel();
}
@Named
public class FooScheduleController extends GenericScheduleController {
private String label;
@Override
public String getLabel() {
return label;
}
public void refreshInit(String label) {
this.label = label;
super.init();
}
}
@Named
@ViewScoped
public class ReportingSchedulerMenuController implements Serializable {
@Inject
private FooScheduleController fooScheduleController;
public GenericScheduleController getScheduleController(String chaine) throws Exception {
if (fooScheduleController.getValue() == null) {
fooScheduleController.refreshInit(chaine);
}
return fooScheduleController;
}
}
Upvotes: -1
Reputation: 4048
I think what you're looking for is Instance:
@Inject
Instance<GenericScheduleController> instances;
public <T extends GenericScheduleController> T getScheduleController(String chaine) {
return instances.stream()
.filter(s -> s.getLabel().equals(chaine))
.findAny()
.orElse(null);
}
Upvotes: 0