복붙노트

[SPRING] 스프링 부트로 보조 서블릿을 등록하려면 어떻게해야합니까?

SPRING

스프링 부트로 보조 서블릿을 등록하려면 어떻게해야합니까?

내 응용 프로그램에 등록해야하는 추가 서블릿이 있습니다. 그러나 Spring Boot와 Java Config에서는 web.xml 파일에 서블릿 매핑을 추가 할 수 없습니다.

서블릿을 추가하려면 어떻게해야합니까?

해결법

  1. ==============================

    1.Servlet을위한 bean을 추가하기 만하면됩니다. / {beanName} /에 매핑됩니다.

    Servlet을위한 bean을 추가하기 만하면됩니다. / {beanName} /에 매핑됩니다.

    @Bean
    public Servlet foo() {
        return new FooServlet();
    }
    
  2. ==============================

    2.ServletRegistrationBean도 사용할 수 있습니다.

    ServletRegistrationBean도 사용할 수 있습니다.

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

    결국 내가했던 길을 끝냈다.

  3. ==============================

    3.여러 개의 다른 서블릿을 Application 클래스의 @Bean과 같은 다른 ServletRegistrationBean과 함께 등록 할 수 있으며 서블릿을 등록 할 수 있습니다.

    여러 개의 다른 서블릿을 Application 클래스의 @Bean과 같은 다른 ServletRegistrationBean과 함께 등록 할 수 있으며 서블릿을 등록 할 수 있습니다.

       @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");
       }
    
  4. ==============================

    4.다음과 같이 Servlet을 등록 할 수도 있습니다.

    다음과 같이 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);
      }
    }
    
  5. ==============================

    5.임베디드 서버를 사용하는 경우 @WebServlet을 사용하여 서블릿 클래스에 주석을 달 수 있습니다.

    임베디드 서버를 사용하는 경우 @WebServlet을 사용하여 서블릿 클래스에 주석을 달 수 있습니다.

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

    @WebServlet에서 :

    그리고 기본 클래스에서 @ServletComponentScan을 활성화하십시오.

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

    @ServletComponentScan은 임베디드 서버에서만 작동합니다.

    추가 정보 : Spring Boot에서 @ServletComponentScan Annotation

  6. ==============================

    6.BeanDefinitionRegistryPostProcessor에서도 사용할 수 있습니다.

    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) {
        }
    }
    
  7. from https://stackoverflow.com/questions/20915528/how-can-i-register-a-secondary-servlet-with-spring-boot by cc-by-sa and MIT license