68

我有一个额外的 servlet 需要在我的应用程序中注册。但是,使用 Spring Boot 及其 Java Config,我不能只在web.xml文件中添加 servlet 映射。

如何添加额外的 servlet?

4

7 回答 7

142

还可以使用ServletRegistrationBean

@Bean
public ServletRegistrationBean servletRegistrationBean(){
    return new ServletRegistrationBean(new FooServlet(),"/someOtherUrl/*");
}

这最终成为我所走的道路。

于 2014-01-05T22:16:28.863 回答
59

只需为 servlet 添加一个 bean。它将被映射到/{beanName}/.

@Bean
public Servlet foo() {
    return new FooServlet();
}
于 2014-01-04T01:17:16.527 回答
22

您可以在 Application 类中使用不同的 ServletRegistrationBean 注册多个不同的 servlet,例如 @Bean ,您可以注册一个具有多个 servlet 映射的 servlet;

   @Bean
   public ServletRegistrationBean axisServletRegistrationBean() {
      ServletRegistrationBean registration = new ServletRegistrationBean(new AxisServlet(), "/services/*");
      registration.addUrlMappings("*.jws");
      return registration;
   }

   @Bean
   public ServletRegistrationBean adminServletRegistrationBean() {
      return new ServletRegistrationBean(new AdminServlet(), "/servlet/AdminServlet");
   }
于 2016-05-05T12:02:15.800 回答
5

我们也可以通过以下方式注册 Servlet:

@Configuration
public class ConfigureWeb implements ServletContextInitializer, EmbeddedServletContainerCustomizer {

  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
      registerServlet(servletContext);
  }

  private void registerServlet(ServletContext servletContext) {
      log.debug("register Servlet");
      ServletRegistration.Dynamic serviceServlet = servletContext.addServlet("ServiceConnect", new ServiceServlet());

      serviceServlet.addMapping("/api/ServiceConnect/*");
      serviceServlet.setAsyncSupported(true);
      serviceServlet.setLoadOnStartup(2);
  }
}
于 2017-02-20T13:58:43.917 回答
4

如果您使用的是嵌入式服务器,则可以使用@WebServlet您的 servlet 类进行注释:

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

来自@WebServlet

用于声明 servlet 的注解。

此注释由容器在部署时处理,并且相应的 servlet 在指定的 URL 模式下可用。

@ServletComponentScan并在基类上启用:

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

请注意,@ServletComponentScan仅适用于嵌入式服务器:

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

更多信息:Spring Boot 中的 @ServletComponentScan 注解

于 2018-03-27T18:29:04.953 回答
2

这种方式对我有用,有一个名为 WS01455501EndpointFor89 的 servlet

@Bean
public ServletRegistrationBean<WS01455501EndpointFor89> servletRegistrationBeanAlt(ApplicationContext context) {
    ServletRegistrationBean<WS01455501EndpointFor89> servletRegistrationBean = new ServletRegistrationBean<>(new WS01455501EndpointFor89(),
            "/WS01455501Endpoint");
    servletRegistrationBean.setLoadOnStartup(1);
    return servletRegistrationBean;
}
于 2019-07-04T22:40:09.297 回答
0

也可在 BeanDefinitionRegistryPostProcessor 中使用

package bj;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@SpringBootApplication
class App implements BeanDefinitionRegistryPostProcessor {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        registry.registerBeanDefinition("myServlet", new RootBeanDefinition(ServletRegistrationBean.class,
                () -> new ServletRegistrationBean<>(new HttpServlet() {
                    @Override
                    protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
                        resp.getWriter().write("hello world");
                    }
                }, "/foo/*")));
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    }
}
于 2018-09-26T06:34:48.900 回答