Vaadin7 + CDI

Hi guys,

is there already a way to use injection with vaadin 7?
I used this guide to do the CDI stuff with Vaadin 6.x
but now the entiy point is Root and not Application so there is no way to copy this guide to Vaadin 7 - any ideas?

Could I bump this question? Do all JEE6 CDI specs work in Vaadin? (Obviously only if Vaadin is hosted in a jee6 spec container.)

JSR-299 Spec


there are two projects that help you with CDI:
The great cdi-utils Addon which shows you how to use CDI in Vaadin 6. Only Root Application acces has changed for Vaadin 7, main design will stay the same.

I have started with a CDI + Vaadin 7 based application framework which is just started and no official addon yet. You can find it here:
With a sample app here:

In this application I use CDI with events, JPA 2. Tomi uses in his cdi-utils also custom scopes and so on. Since you program inside your container, everything will work until you start to work inside widgets.


You can find the default setup for V7 apps for CDI (as for V6) over
In the RootApplication you can define all of you beans.

The only change thereafter is then to change your Root to pick up the beans by using getApplication().get()


? I don’t get it - sorry!

Assuming that this is the basic we have:

public class MyApplicationRoot extends Root {

    public static class RootApplication extends Application {
        private static final long serialVersionUID = -1099516579868329607L;
        protected Root getRoot(WrappedRequest request) throws RootRequiresMoreInformationException {
            return new MyApplicationRoot();
    public static class Servlet extends AbstractApplicationServlet {
        private static final long serialVersionUID = -1386739418720898885L;

        protected Application getNewApplication(HttpServletRequest request) throws ServletException {
            return new RootApplication();

        protected Class<? extends Application> getApplicationClass() throws ClassNotFoundException {
            return RootApplication.class;
    private static final long serialVersionUID = 7428504147931595620L;

    protected void init(WrappedRequest request) {
        VerticalLayout view = new VerticalLayout();
        view.addComponent(new Label("Hello Vaadin!"));

how do we have to modify this code to enable CDI for at least the MyApplicationRoot class?

I just started a blog about Vaadin 7 and CDI (Guice here). Maybe this helps.

Have fun,

Also would like to note Adam Bien’s Weblog about CDI and Vaadin.

Something similar should work with Vaadin 7 too .

I did this with Vaadin 6.8 and it works like a charm…

Hi there,

I tried to include Vaadin 7 beta 5 in a Multi-Module Maven Application with a war, jar and ear that will be deployed to JBoss AS 7.1.1.

So i tried following Code, but somehow CDI is not working.
The Variables that have @Inject are Null and i get a NullPointer Exception.

I have following FileStructure:


  • src/main/resources/WEB-INF
    • beans.xml
    • web.xml

and in the app-jar

  • src/main/resources
    - beans.xml
    - persistence.xml (Hibernate)

Got follwongin Contents:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi=""


public class CdiUiProvider extends DefaultUIProvider implements Serializable{
	private static final long serialVersionUID = 8043964411848504777L;

	private VaadinUI vaadinUI;

	public Class<? extends UI> getUIClass(UIClassSelectionEvent event) {
		return VaadinUI.class;

	public UI createInstance(UICreateEvent event) {
		return vaadinUI;


My own CdiVaadinServlet

public class CdiVaadinServlet extends VaadinServlet{

	private static final long serialVersionUID = -8833005846998449164L;
	private Instance<CdiUiProvider> applicationProvider;
	private final SessionInitListener sessionInitListener = new SessionInitListener() {
		public void sessionInit(SessionInitEvent event) throws ServiceException {
			VaadinService service = event.getService();
            final VaadinServiceSession session = event.getSession();
	public void init(ServletConfig servletConfig) throws ServletException {


The View

public class VaadinUI extends UI implements ClickListener {
	private static final long serialVersionUID = 1L;
	private ShopUI shopUI;
	protected void init(VaadinRequest request) {
	private void buildMainLayout() {
		getPage().setTitle("Vaadin UI");

		layout = new VerticalLayout();



problem is that shopUI is null and therefore nothing is set.
And also when i set a breakPoint in the Servlet nothing happens there so still think i ueses the Default-VaadinServlet instead of mine.

here is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	xmlns="" xmlns:web=""
	id="WebApp_ID" version="2.5">
	<display-name>Vaadin Web Application</display-name>
		<description>Vaadin production mode</description>

		<servlet-name>Vaadin CDI Servlet</servlet-name>
			<description>Vaadin UI to display</description>

anybody an Idea what goes wrong?
I really would like to use jee6 as the backend, but that is useless if i can not inject the backend with the logic


I also tried this GitHubProject without Luck

anybody knows on how i have to config this one to get it wrork?

Sorry bit clueless here

found my Problem.

the Project had to web.xml, one under resources and one in webapp. deleted the resources one and configured the one in webapp. within the same war it works now.
only Injections to other jars are not working yet.

Hi Marc,

at the first glance your solution seems to work. But when I pressed “reload” in my browser and got an exception.

javax.servlet.ServletException: java.lang.IllegalStateException: UI id has already been defined

I am not sure whether it is correct to annotate the VaadinUI class as @ApplicationScoped, since Book of Vaadin states

“A Vaadin application is essentially a user session, and a session is created for each user who uses the application. In the context of our HelloWorld application, it is sufficient to know that the underlying session is created when the user first accesses the application by opening the page, and init() method is invoked at that time.”

I would assume @SessionScoped is more appropriate here but I get the same Exception when using it…

I think @SessionScoped and @ApplicationScoped in vaadin 7 is more or less the same thing (Not identical, but by default they should resemble the same scope.
You need a custom scope for your VaadinUI implementation since you can have multiple VaadinUI instances ( multiple tabs or windows ) for the same session and application (depending on your application design).

In cdi-utils addon Tomi uses @VaadinScoped( VaadinScope.WINDOW) to tie the component to the current request via @RequestScoped , you will probably need the same approach for VaadinUI since VaadinUI is more synonymous with Window in vaadin 6.x from my understanding.

The UI’s should be annotated with @VaadinUI which is a scope in itself, and if you only have one UI in it, then annotate that want also with @Root.

Clarification. In Vaadin 6 anyways when an Application closes Vaadin does not invalidate the Session. I think they added a policy in Vaadin 7 to close the session (since feature request was closed: but I haven’t verified it.

In Vaadin 6 the same session is used if a new Application is created in the same window so you need to be careful about what is bound to the session scope.

Building snapshot … fails with selenium


I cloned the project from github and start “mvn clean install”. Selenium tests are integrated, firefox starts and the build fails. Is there a way to skip selenium for local builds? Is there an official release?


The official release is coming a little later (currently plan is beta in March, final in April) - there will be some changes, most likely including a better UI scope that allows injecting components into injected components etc.

You should be able to skip tests the usual way - if I remember correctly:

-DskipTests to skip running unit tests or -Dmaven.test.skip=true to also skip compiling them, -DskipITs to skip integration tests. There are similar properties (with somewhat different names) to the failsafe plug-in if you want to configure these in your POM.

Just in case anyone is still struggling with Guice and Vaadin, David Sowerby and myself have created an addon which hopefully should make it easier.

CDI with Java EE 7 always throws NPE. can you please provide us a full working solution or a video on how we can configure that.

Don’t know where you’re doing your injections and having the NPE, but I’m using Vaadin 7.4 alpha with Java EE 7 and CDI is working just fine. You might want to elaborate. Also you can simply use Adam Bien’s POM in this simple layout and build on it. It works just fine

Just clone and use as template for Vaadin 7 with Java EE 7 as backend

I’m doing the injection of the EJB in the MyVaadin class which extends UI and i have also added the CDI adds-on. I don’t know what i 'm doing wrong here. If possible can you explain me in simple steps what you did in configuration in order to achieve that. I’m using NetBeans 8 and Vaadin plugin. If possible show some configuration steps you took to succed.