Spring boot convert web.xml listener
Asked Answered
M

3

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?

Molnar answered 17/2, 2015 at 16:33 Comment(0)
C
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

Cloakanddagger answered 17/2, 2015 at 16:43 Comment(7)
You mean that also listeners in my external jars are automatically loaded? <listener> <listener-class>org.granite.config.GraniteConfigListener</listener-class> </listener> <listener> <listener-class>org.granite.gravity.websocket.GravityWebSocketDeployer</listener-class> </listener>Molnar
No that are not register automatically only both that spring uses, but you use use servletContext.addListener to add them, let me update the answerCloakanddagger
createRootApplicationContext() method is not inherited from WebApplicationInitializer.Molnar
Read this link for further information docs.spring.io/spring-boot/docs/current/reference/html/…Cloakanddagger
Change it for an abstract class extends SpringBootServletInitializer, but first read the link in the previous postCloakanddagger
Finally according to the specs the injection could be done using just @Bean as this link implies docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/…Cloakanddagger
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".Backstairs
B
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();
}
Backstairs answered 17/2, 2015 at 17:0 Comment(2)
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...Doggone
Yes, it will work when deploying to a standalone container as well.Backstairs
C
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

Cloakanddagger answered 17/2, 2015 at 16:43 Comment(7)
You mean that also listeners in my external jars are automatically loaded? <listener> <listener-class>org.granite.config.GraniteConfigListener</listener-class> </listener> <listener> <listener-class>org.granite.gravity.websocket.GravityWebSocketDeployer</listener-class> </listener>Molnar
No that are not register automatically only both that spring uses, but you use use servletContext.addListener to add them, let me update the answerCloakanddagger
createRootApplicationContext() method is not inherited from WebApplicationInitializer.Molnar
Read this link for further information docs.spring.io/spring-boot/docs/current/reference/html/…Cloakanddagger
Change it for an abstract class extends SpringBootServletInitializer, but first read the link in the previous postCloakanddagger
Finally according to the specs the injection could be done using just @Bean as this link implies docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/…Cloakanddagger
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".Backstairs
F
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;
   }
Floe answered 5/5, 2016 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.