← Back

Guice Vaadin integration

Vaadin integration for Google Guice





Guice Vaadin is the official Guice integration for Vaadin Framework.


setting up the servlet

first step is to set up the GuiceVaadinServlet, which needs a packagesToScan parameter holding the names of all packages that should be scanned for UIs, Views, ViewChangeListeners and VaadinServiceInitListeners. Sub-packages of these packages are scanned as well.

This can be done either by subclassing GuiceVaadinServlet and annotating it with @PackagesToScan, or by configuring a GuiceVaadinServlet in the deployment-descriptor.

configuration in java

    package org.mypackage;

    @javax.servlet.annotation.WebServlet(name = "Guice-Vaadin-Servlet", urlPatterns = "/*")
    @com.vaadin.guice.annotation.PackagesToScan({"org.mycompany.ui", "org.mycompany.moreui"})
    public class MyServlet extends com.vaadin.guice.server.GuiceVaadinServlet{

configuration in xml

<web-app xmlns="" version="2.5">
            <param-value>org.mycompany.ui, org.mycompany.moreui</param-value>


Available scopes are UIScope and VaadinSessionScope, similar to what the Spring addon offers. UIScope is what MUST be the scope for all Vaadin-components, since they must belong to exactly one UI. VaadinSessionScope may be used to sync data between multiple tabs in the same browser.

import com.vaadin.guice.annotation.UIScope;

public class MyButton extends Button {

Guice-Module loading

Since Guice is configured via so called Modules, we need a way to load these modules. All Modules in the packages contained by 'packagesToScan' will be instantiated and loaded by default.

package org.mycompany.ui;


//will be loaded, since the 'org.mycompany.ui'-package is included in
//the packagesToScan  
public class MyModule extends AbstractModule{
    protected void configure(){

The alternative way to load modules is via the @Import-Annotation. This was introduced to make sort of an addon-development for guice-vaadin possible, similar to what Spring offers with it's own @Import-annotation.

First, a new Annotation is needed that points to the module to be loaded via @Import:

package org.mycompany.ui;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

public @interface UseSomeModule {    

Second, the annotation is pinned on the servlet and that's it.

    package org.mypackage;

    @javax.servlet.annotation.WebServlet(name = "Guice-Vaadin-Servlet", urlPatterns = "/*")
    @com.vaadin.guice.annotation.PackagesToScan({"org.mycompany.ui", "org.mycompany.moreui"})
    public class MyServlet extends com.vaadin.guice.server.GuiceVaadinServlet{

Copyright 2015-2017 Vaadin Ltd.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.


possible NPE fix

Apache License 2.0


Vaadin 8.0+
Vaadin 7.0+ in 1.0.0
Vaadin 7.1+ in 1.1.4
Vaadin 7.6+ in 1.2.0-final
Vaadin 11+ in 2.3.6-vaadin10
Vaadin 10+ in 2.0.1-vaadin10
Vaadin 14+ in 2019.1-vaadin14

Vaadin Add-on Directory

Find open-source widgets, add-ons, themes, and integrations for your Vaadin application. Vaadin Add-on Directory
The channel for finding, promoting, and distributing Vaadin add-ons.