cdi
cdi copied to clipboard
Support injection of components read from a declarative file
https://github.com/vaadin/spring/pull/193 added support for Vaadin Spring to create components in a design as Spring beans. The same thing would be needed for CDI so you can inject the application components instead of always creating them using new MyComponent
This seems to work but I hope there is a better way:
Design.setComponentFactory(new DefaultComponentFactory() {
@Override
public Component createComponent(String fullyQualifiedClassName, DesignContext context) {
Class<? extends Component> componentClass = resolveComponentClass(fullyQualifiedClassName, context);
Instance<? extends Component> managedComponent = CDI.current().select(componentClass);
if (!managedComponent.isAmbiguous() && !managedComponent.isUnsatisfied()) {
// Injectable, still not clear if the correct class will be
// used (e.g. componentClass might be Grid<Foo> and
// MyOwnGrid might be injected, if that happens to exist in
// the project
Component component = managedComponent.get();
if (component.getClass() == componentClass) {
return component;
}
}
return super.createComponent(fullyQualifiedClassName, context);
}
});