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.
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.
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()
}
Here is my UI class:
@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 {
}
}
My root-context.xml file is in the directory /WEB-INF/spring/root-context.xml.
The applicationContext.xml for the Vaadin servlet is in the directory /WEB-INF/spring/vaadin/applicationContext.xml.
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.
<?xml version="1.0" encoding="UTF-8"?>
<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">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
</web-app>
Vaadin Configuration class:
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;
}
}