Run code in Vaadin 8 application idependent of UI

╄→尐↘猪︶ㄣ 提交于 2019-12-11 15:23:51

问题


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?


回答1:


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

}


来源:https://stackoverflow.com/questions/49531773/run-code-in-vaadin-8-application-idependent-of-ui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!