How to use CDI on domain objects?

Hi,

how can I use CDI on domain objects?

I’ve successfully set up the CDI AddOn and already using @CDIUI, the application runs fine.
Now I tried the following, but the object is never injected. Why?


@UiScoped
class UserSettings;

@UiScoped
class User {
	@Inject
	@New
	private UserSettings settings; //always null
}

This is my servlet:


@CDIUI
public class MyApp extends UI {
    @WebServlet(value = "/*", asyncSupported = true,
    	initParams = { @WebInitParam(name = Constants.SERVLET_PARAMETER_UI_PROVIDER, value = "com.vaadin.cdi.CDIUIProvider")})
    @VaadinServletConfiguration(productionMode = false, ui = MyApp.class, widgetset = "my.app.AppWidgetSet")
    public static class Servlet extends VaadinServlet {}
    
}

I also tried to inject a vaadin component in a class (same as what has been done here:
https://github.com/vaadin/cdi/blob/master/vaadin-cdi-example/src/main/java/com/vaadin/cdi/example/view/InjectableLayout.java
), but also this gives me always a NPE:


class MyMenuBar extends CustomComponent {
	@Inject
	@New
	private MenuBar menuBar; //is always null!
}

You shouldn’t need a WebServlet annotation, and it might actually mess up your deployment. Just use @CDIUI.

I’m not familiar with the @New annotation (not that familiar with CDI in general), but injecting thins into the UI should work after you do the above.

I saw the CDI example without the WebServlet annotation but just curious. Without the VaadinServlet how do we provide configuration for Push and Vaadin?

There are still some problems/limitations with CDI and push. I hope we can resolve these for Vaadin CDI 1.0.0.beta1.

If you define any servlet manually (whether with @WebServlet or with a web.xml file), ContextDeployer will detect it and will not automatically deploy its VaadinServlet. In this case, make sure you use CDIUIProvider (or its subclass) in your custom servlet.