Spring Boot:将Bean注入HttpServlet

时间:2017-04-27 09:57:33

标签: spring spring-boot

我有一个Spring Boot,我已经自动配置了一个Router bean。 这一切都很完美,但当我想将bean注入自定义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();
    }
}

这应该是要走的路,但是

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

不会自动装配路由器,因为WebapplicationContext始终为空。该应用程序在MVC环境中运行。

3 个答案:

答案 0 :(得分:4)

假设您将Spring Application Context连接到Servlet Context,您可能希望将ServletContext传递给https://www.typescriptlang.org/

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

答案 1 :(得分:1)

如何将'Router'作为构造函数参数注入。

所以你会有这个:

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

然后您可以以编程方式创建这样的servlet注册:

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

答案 2 :(得分:1)

嵌入式服务器

您可以使用@WebServlet servlet类进行注释:

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

在基类上启用@ServletComponentScan

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

但注入@ServletComponentScan仅适用于嵌入式服务器:

  

启用对Servlet组件的扫描(过滤器,servlet和   听众)。仅在使用嵌入式Web时执行扫描   服务器

更多信息:The @ServletComponentScan Annotation in Spring Boot

外部服务器

使用外部服务器时,将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");
    }
}