cdi icon indicating copy to clipboard operation
cdi copied to clipboard

Support injection of components read from a declarative file

Open Artur- opened this issue 7 years ago • 1 comments

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

Artur- avatar Jun 30 '17 10:06 Artur-

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);
			}
		});

Artur- avatar Jul 05 '17 13:07 Artur-