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;
}
}
applicationContext.xmlis a common name used for XML-defined spring context, but you can name it whatever you want, so no, if you already have yourrootcontext.xmlyou don't need to create the other one. One thing you could try, is to migrate to annotation based contexts. By no means that's necessary and it's a personal preference, but I find it more helpful to view info while writing my code than having to go to the XML definition, even if nowadays IDEs have good spring support for such things.root-contextreferenced inweb.xml, and import yourapplicationContextin yourroot-contextas described in the previous link about splitting a context. If I were you, I'd fully migrate to an annotation-based context (you already have a few such beans and configs), and not worry about XMLs any more. Spring boot or not it's up to you, and the vaadin tutorial should offer sufficient info about how to bootstrap it and use it.