10

I'm trying to convert my project to Spring Boot project (executable jar file with Jetty embedded). All works with a standard example but I want migrate my old web.xml to Spring Boot.

I migrated Servlet and Filters but I don't understand how migrate filters as this:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
    <listener-class>org.granite.config.GraniteConfigListener</listener-class> 
</listener> 
    <listener>
    <listener-class>org.granite.gravity.websocket.GravityWebSocketDeployer</listener-class>
</listener>

I created my @SpringBootApplication class and I wrote inside all the configuration:

@Bean
@Order(1)
public FilterRegistrationBean springSecurityFilterChain() {     
    FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
    DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy();
    filterRegBean.setFilter(delegatingFilterProxy);
    List<String> urlPatterns = new ArrayList<String>();
    urlPatterns.add("/*");
    filterRegBean.setUrlPatterns(urlPatterns);
    return filterRegBean;
}

Someone can explain me how Listeners should be converted?

rajadilipkolli
  • 3,475
  • 2
  • 26
  • 49
drenda
  • 5,846
  • 11
  • 68
  • 141

3 Answers3

12

Spring Boot will automatically register any @Beans of the following types with the servlet container:

  • ServletContextAttributeListener
  • ServletRequestListener
  • ServletRequestAttributeListener
  • HttpSessionAttributeListener
  • HttpSessionListener
  • ServletContextListener

For example, to register GravityWebSocketDeployer which is a ServletContextListener add a @Bean method to your configuration class:

@Bean
public GravityWebSocketDeployer gravityWebSocketDeployer() {
    return new GravityWebSocketDeployer();
}
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • Section 27.3 of the current Spring Boot documentation mentions this with regard to the Embedded Tomcat servlet container, but is this true also for a standalone servlet container? I don't see any documentation to that effect, but it seems to work for me on a stand-alone Tomcat instance... – Mike Muske Aug 03 '17 at 20:03
  • Yes, it will work when deploying to a standalone container as well. – Andy Wilkinson Aug 03 '17 at 20:08
4

For RequestContext read this

 @Bean
    @ConditionalOnMissingBean(RequestContextListener.class)
    public RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }

For the other listener is register automatically when you use spring-boot as this link implies.

For your own listeners.

public class MyAdditionListeners extends SpringBootServletInitializer {

    protected final Log logger = LogFactory.getLog(getClass());

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
        if (rootAppContext != null) {
            servletContext.addListener(new YourListenerHere());
        }
        else {
            this.logger.debug("No ContextLoaderListener registered, as "
                    + "createRootApplicationContext() did not "
                    + "return an application context");
        }
    }

Finally there is a link in which you can find some information about listeners and SpringApplication class. Read section

Koitoer
  • 18,778
  • 7
  • 63
  • 86
  • You mean that also listeners in my external jars are automatically loaded? org.granite.config.GraniteConfigListener org.granite.gravity.websocket.GravityWebSocketDeployer – drenda Feb 17 '15 at 16:46
  • No that are not register automatically only both that spring uses, but you use use servletContext.addListener to add them, let me update the answer – Koitoer Feb 17 '15 at 16:49
  • createRootApplicationContext() method is not inherited from WebApplicationInitializer. – drenda Feb 17 '15 at 16:55
  • Read this link for further information http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html#boot-features-application-events-and-listeners – Koitoer Feb 17 '15 at 16:55
  • Change it for an abstract class extends SpringBootServletInitializer, but first read the link in the previous post – Koitoer Feb 17 '15 at 16:56
  • Finally according to the specs the injection could be done using just @Bean as this link implies http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-add-a-servlet-filter-or-servletcontextlistener – Koitoer Feb 17 '15 at 16:59
  • Using `SpringBootServletInitializer` when you're using an embedded container is a little unusual. As stated in its javadoc, it's intended "to run a SpringApplication from a traditional WAR deployment". – Andy Wilkinson Feb 17 '15 at 17:02
0

Also Spring Boot will automatically register any @Bean extend of HttpServlet;

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