使用 Spring Boot 在 Vaadin 中创建一个 servlet 以添加会话侦听器

Creating a servlet in Vaadin with Spring Boot to add session listeners

我正在尝试在 Spring 启动时在 Vaadin 14 运行 中创建一个 servlet。我已阅读 Vaadin Application lifecycle 上的应用程序生命周期页面,并尝试借助它创建一个 servlet,但没有成功。我的网页似乎无法识别 URL。我已经尝试了 VaadinServlet 和 SpringServlet,这是我的代码:

@WebServlet(value = "name", urlPatterns = {"customservlet"})
public class CustomServlet extends SpringServlet implements SessionInitListener, SessionDestroyListener {

    public CustomServlet(ApplicationContext context, boolean forwardingEnforced) {
        super(context, forwardingEnforced);
    }

    @Override
    protected void servletInitialized() throws ServletException {
        super.servletInitialized();
        getService().addSessionInitListener(this);
        getService().addSessionDestroyListener(this);
    }

    @Override
    public void sessionInit(SessionInitEvent event)
            throws ServiceException {
        // Do session start stuff here
    }

    @Override
    public void sessionDestroy(SessionDestroyEvent event) {
        // Do session end stuff here
    }


    @Override
    protected void service(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {

        getService().addSessionInitListener((SessionInitListener) event -> event.getSession().addRequestHandler(globalRequestHandler()));
        super.service(request, response);
    }

    public RequestHandler globalRequestHandler() {
        return (session, vaadinRequest, response) -> {
            if ("/customservlet".equals(vaadinRequest.getPathInfo())) {
                response.setContentType("text/html;charset=UTF-8");
                response.getWriter().append("some content");
                response.getWriter().flush();
                return true;
            } else  {
                return false;
            }
        };
    }
}

Edit: It worked with Leifs with solution. Here are the results if anyone wants to know:

@Route("yourroute")
@SpringComponent
public class MyInitListener implements Serializable, VaadinServiceInitListener  {

    @Override
    public void serviceInit(ServiceInitEvent serviceEvent) {
        serviceEvent.getSource().addSessionInitListener(initEvent -> {
            initEvent.getSession().addRequestHandler(globalRequestHandler());

        });
        serviceEvent.getSource(). addSessionDestroyListener(initEvent -> {
            initEvent.getSession().close();
        });
    }

public RequestHandler globalRequestHandler() {
        return (session, vaadinRequest, response) -> {
            if ("/yourroute".equals(vaadinRequest.getPathInfo())) {
                response.setContentType("text/plain");
                PrintWriter pw = response.getWriter();
                pw.write("Some content");
                pw.close();
                return true;
            } else  {
                return false;
            }
        };
    }
}

我将尝试回答您似乎想要实现的事情(添加会话侦听器),而不是您尝试这样做的方式(创建自定义 servlet)。

添加这些侦听器的最简单方法是使用 VaadinServiceInitListener。使用 Spring 时,您只需定义一个实现该接口的 bean 即可。

@Component
public class MyInitListener implements VaadinServiceInitListener {
  @Override
  public void serviceInit(ServiceInitEvent serviceEvent) {
    serviceEvent.getSource().addSessionInitListener(initEvent -> {
      // Do session start stuff here      
    });
    serviceEvent.getSource(). addSessionDestroyListener(initEvent -> {
      // Do session end stuff here      
    });
  }
}