【发布时间】:2017-10-18 17:49:22
【问题描述】:
我在将 Vaadin 与我的 Spring 应用程序集成时遇到了一些问题。我的所有 bean 都在“rootcontext.xml”文件中。我可以通过实例化“rootcontext.xml”然后为我的服务类之一调用 bean 来调用 bean。
我可以通过这种方式填充表格,但这是调用服务类的正确方式吗?因为我有更多的表必须调用它。
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()
}
这是我的 UI 类:
@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 {
}
}
我的 root-context.xml 文件在目录 /WEB-INF/spring/root-context.xml 中。
Vaadin servlet 的 applicationContext.xml 位于目录 /WEB-INF/spring/vaadin/applicationContext.xml 中。
这是我的 web.xml。 Vaadin Spring 教程说使用上下文加载器来初始化“applicationContext.xml”。我可以将其路径添加到 contextConfigLocation 参数,但应该只有一个根上下文。
<?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 配置类:
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;
}
}
【问题讨论】:
-
您是否有机会了解较新的Vaadin-spring tutorial 或较旧的wiki section?为了保持干净,表格应该只显示数据,其他人应该调用服务并向表格提供(填充)信息
-
applicationContext.xml是用于 XML 定义的 spring 上下文的通用名称,但您可以随意命名,所以不,如果您已经拥有rootcontext.xml,则无需创建另一个。您可以尝试的一件事是迁移到基于注释的上下文。这绝不是必要的,这是个人喜好,但我发现在编写代码时查看信息比必须转到 XML 定义更有帮助,即使现在 IDE 对这些东西有很好的 Spring 支持。 -
根据您的评论,我假设您正在实例化 2 个不同的弹簧上下文。通常有一个这样的上下文包含所有 bean 定义,因此它可以在需要时正确地注入它们(除非您需要 hierarchy)。在 2 个不同的上下文中,一个人不知道另一个上下文中的内容。你可以做的是split it into smaller parts and import them in your root context。
-
保持原样,在
web.xml中引用root-context,并在root-context中导入您的applicationContext,如previous link about splitting a context 中所述。如果我是你,我会完全迁移到基于注释的上下文(你已经有一些这样的 bean 和配置),而不再担心 XML。春季启动与否取决于您,vaadin tutorial 应提供有关如何启动和使用它的足够信息。 -
你也可以从他们简单的vaadin-spring integration demo app中得到一些灵感,基于spring boot。