A simple Vaadin project with CDI

Hi all,

I have create a new Vaadin 7 project using Vaadin Eclipse plug-in (the project uses Ivy). I created the empty beans.xml in WebContent/META-INF. I have modified my UI class in the following way:

@CDIUI
public class MyUI extends UI {

  @WebServlet(value = "/*",
              asyncSupported = true,
              initParams = {
                @WebInitParam(name = "UIProvider", value = "com.vaadin.cdi.CDIUIProvider")
              })
  @VaadinServletConfiguration(productionMode = false, ui = MyUI.class)
  public static class Servlet extends VaadinServlet {}

  @Inject
  DummyService service;

  @Override
  protected void init(VaadinRequest request) {

    final VerticalLayout layout = new VerticalLayout();
    layout.setMargin(true);
    setContent(layout);

    Button button = new Button("Click Me");
    button.addClickListener(new Button.ClickListener() {
      public void buttonClick(ClickEvent event) {
        layout.addComponent(new Label(service.itWorks()));
      }
    });

    layout.addComponent(button);
  }
}

My DummyService class is the following:

[code]
@Stateless

public class DummyService {

public String itWorks() {
return “It works!”;
}
}
[/code]I have also added



in ivy.xml.

But, when clicking on the button I get a NullPointerException,
service
is
null
.

I’m using TomEE with no particular configuration. What am I doing wrong?

Thanks for the help!

There’s no need to define your own servlet when using the CDI add-on. Remove the servlet and my guess is that it will start working.

The below annotation runs on Wildfly, when I remove it deployment fails.

@WebServlet(urlPatterns = “/*”, initParams = {
@WebInitParam(name = VaadinSession.UI_PARAMETER, value = “com.xxx.MyVaadinUI”),
@WebInitParam(name = Constants.SERVLET_PARAMETER_UI_PROVIDER, value = “com.vaadin.cdi.CDIUIProvider”)
})

public static class UIApplicationServlet extends VaadinServlet {
}

I really can’t put my finger on what’s wrong with TS’ code…