Servlet dependency injection on embedded Jetty

I have a built-in Jetty server and I added a servlet mapping.

ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.addServlet(RegisterServlet.class, "/user/register");

I want to do dependency injection in servlets with spring framework customizing ApplicationContext.xml. It should work just like here:

public class RegisterServlet extends HttpServlet {
private Service service;
@Override
public void init() throws ServletException {
    super.init();
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    service = context.getBean("service", Service.class);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ...
}

but without using context.getBean ("service").

+4
source share

Source: https://habr.com/ru/post/1599749/


All Articles