Spring Download: Inject Bean in HttpServlet

I have a Spring Boot where I autoconfigure a Router bean. All this works fine, but it becomes a problem when I want to enter a bean in a user servlet:

public class MembraneServlet extends HttpServlet {
    @Autowired
    private Router router;

    @Override
    public void init() throws ServletException {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        new HttpServletHandler(req, resp, router.getTransport()).run();
    }
}

It should be the way, but

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

will not authorize the router since it is WebapplicationContextalways null. The application runs in an MVC environment.

+4
source share
3 answers

Assuming you are a Spring application context connected to a servlet context, you can pass the ServletContext to SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext

public class MembraneServlet extends HttpServlet {

  @Autowired
    private Router router;

    @Override
    public void init() throws ServletException {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this, getServletContext());
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        new HttpServletHandler(req, resp, router.getTransport()).run();
    }
}
+3
source

"" .

, :

public class MembraneServlet extends HttpServlet {

    private Router router;

    public MembraneServlet(Router router){
        this.router = router;
    }

    @Override
    public void init() throws ServletException {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        new HttpServletHandler(req, resp, router.getTransport()).run();
    }
}

:

@Bean
public ServletRegistrationBean membraneServletRegistrationBean(){
    return new ServletRegistrationBean(new MembraneServlet(),"/*");
}
+1

@WebServlet :

@WebServlet(urlPatterns = "/example")
public class ExampleServlet extends HttpServlet 

@ServletComponentScan :

@ServletComponentScan
@EntityScan(basePackageClasses = { ExampleApp.class, Jsr310JpaConverters.class })
@SpringBootApplication
public class ExampleApp 

@ServletComponentScan :

(, ). .

: @ServletComponentScan Spring

HttpServlet @Component:

@Component
public class ExampleServlet extends HttpServlet 

:

@Configuration
@ComponentScan(value = "com.example.servlet.package")
public class ServletConfig {

    @Autowired
    private ExampleServlet exampleServlet;

    @Bean
    public ServletRegistrationBean resetServletRegistrationBean(){
        return new ServletRegistrationBean(exampleServlet, "/example");
    }
}
+1

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


All Articles