在依赖于UI的Vaadin 8应用程序中运行代码

在依赖于UI的Vaadin 8应用程序中运行代码,vaadin,vaadin8,Vaadin,Vaadin8,在早期版本中,可以有一个实现ServletContextListener的类,并将代码放在contextInitialized方法中,以便在服务器启动时运行。这对于将数据库加载到内存中很有用。如何在Vaadin 8项目中实现这一点 方法完全相同:注册一个ServletContextListener。您可以为此使用@WebListener注释。例如: public class WebConfig { @WebServlet("/*") @VaadinServletConfigur

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

方法完全相同:注册一个
ServletContextListener
。您可以为此使用
@WebListener
注释。例如:

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();
        }
    }

}

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

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();
        }
    }

}

你准备了什么?春天还是普通web应用程序?您设置了什么?春天或者普通web应用程序?请注意,在使用Spring Boot时,您可能还必须在应用程序上使用
@ServletComponentScan
注释,以使其实际拾取带有注释的类,例如
@WebListener
。我缺少注释。谢谢你,亚历杭德罗!请注意,在使用Spring Boot时,您可能还必须在应用程序上使用
@ServletComponentScan
注释,使其实际拾取带有注释的类,例如
@WebListener
。我缺少注释。谢谢你,亚历杭德罗!