在独立于 UI 的 Vaadin 8 应用程序中运行代码 [英] Run code in Vaadin 8 application idependent of UI

查看:35
本文介绍了在独立于 UI 的 Vaadin 8 应用程序中运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在早期版本中,您可以有一个实现 ServletContextListener 的类,并将您的代码放在 contextInitialized 方法中,以便它在服务器启动时运行.这对于将数据库加载到内存中很有用.如何在 Vaadin 8 项目中实现这一目标?

In earlier versions, you could have a class which implements ServletContextListener and put your code in the contextInitialized method, so that it runs when the server starts. This is useful for loading up the database into memory. How does one achieve this in a Vaadin 8 project?

推荐答案

以完全相同的方式:通过注册一个 ServletContextListener.您可以为此使用 @WebListener 注释.例如:

In exactly the same way: By registering a ServletContextListener. You can use the @WebListener annotation for this. For example:

public class WebConfig {

    @WebServlet("/*")
    @VaadinServletConfiguration(ui = VaadinUI.class, productionMode = false)
    public static class JdbcExampleVaadinServlet extends VaadinServlet {
    }

    @WebListener
    public static class JdbcExampleContextListener implements ServletContextListener {

        @Override
        public void contextInitialized(ServletContextEvent sce) {
            try {
                DatabaseService.init();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            DatabaseService.shutdown();
        }
    }

}

这篇关于在独立于 UI 的 Vaadin 8 应用程序中运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆