title: How to properly integrate Vaadin with Spring Framework?
body: <p>I'm having some trouble integrating Vaadin with my Spring application. I have all my beans in the "rootcontext.xml" file. I can call the beans by instantiating the "rootcontext.xml" then calling the bean for one of my service classes.</p>
  
  <p>I can populate the table this way but is this the right way of calling the service class? Because I have more tables that have to call this.  </p>
  
  <pre><code>public final class TestTable extends Table {
  
  private ApplicationContext applicationContext = (ApplicationContext) VaadinServlet.getCurrent().getServletContext()
          .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
  
  private Service service = this.applicationContext.getBean("service",
          Service.class);
  
  public TestTable() {
      service.findAll()
  }
  </code></pre>
  
  <p>Here is my UI class:</p>
  
  <pre><code>@SpringUI
  @Theme("dashboard")
  @Widgetset("vaadin.DashboardWidgetSet")
  public class TestUI extends UI {
  
  /**
   * 
   */
  private static final long serialVersionUID = -620721219079395670L;
  
  private final DashboardEventBus dashboardEventbus = new DashboardEventBus();
  
  @Override
  protected void init(VaadinRequest request) {
  
      setLocale(Locale.US);
  
      DashboardEventBus.register(this);
      Responsive.makeResponsive(this);
      addStyleName(ValoTheme.UI_WITH_MENU);
  
      updateContent();
  
      // Some views need to be aware of browser resize events so a
      // BrowserResizeEvent gets fired to the event bus on every occasion.
      Page.getCurrent().addBrowserWindowResizeListener(new BrowserWindowResizeListener() {
          @Override
          public void browserWindowResized(final BrowserWindowResizeEvent event) {
              DashboardEventBus.post(new BrowserResizeEvent());
          }
      });
  }
  
  private void updateContent() {
      setContent(new MainView());
  }
  
  @WebServlet(urlPatterns = { "/TestUI/*", "/VAADIN/*" }, name = "TestUIServlet", asyncSupported = true)
  @VaadinServletConfiguration(ui = TestUI.class, productionMode = false)
  public static class TestUIServlet extends VaadinServlet {
  
  }
  }
  </code></pre>
  
  <p>My root-context.xml file is in the directory /WEB-INF/spring/root-context.xml. </p>
  
  <p>The applicationContext.xml for the Vaadin servlet is in the directory /WEB-INF/spring/vaadin/applicationContext.xml.</p>
  
  <p>And here is my web.xml. The Vaadin Spring tutorial says to use the context loader to initialize 'applicationContext.xml'. I could add its path to the contextConfigLocation param but there should only be one root context. </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  id="WebApp_ID" version="3.1"&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/spring/root-context.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Vaadin Configuration class:</p>
  
  <pre><code>import org.springframework.context.annotation.Configuration;
  
  import com.vaadin.spring.annotation.EnableVaadin;
  
  @Configuration
  @EnableVaadin
  public class VaadinConfiguration {
  
  @Autowired
  private Service service;
  
  @Bean
  public UI ui() {
      System.out.println(service.findAll().size());
  
      TestUI testUI = new TestUI();
  
      testUI.setService(service);
  
      return testUI;
  }
  }
  </code></pre>