Uploaded image for project: 'RESTEasy'
  1. RESTEasy
  2. RESTEASY-632

Spring context refresh doesn't work anymore

XMLWordPrintable

      With RESTeasy 2.3 the behavior how Spring beans are registered has changed. In RESTeasy 2.2.2.GA everything was done in SpringBeanProcessor.postProcessBeanFactory() defined by BeanFactoryPostProcessor. The interface and method is still there, but the scanning for JAX-RS annotated beans now happens in SpringBeanProcessor.onApplicationEvent(ApplicationEvent). SpringBEanProcessor registers itself for {{ContextRefreshedEvent|http://static.springsource.org/spring/docs/3.0.6.RELEASE/javadoc-api/org/springframework/context/event/ContextRefreshedEvent.html}}s.

      For some special reasons we have to call refresh() on our Spring Context. Since the ContextRefreshedEvent is send after initialization and through our call, onApplicationEvent(ApplicationEvent) is called twice. This behavior is exact as described by Spring documentation in section 3.13.2 Standard and Custom Events.
      Sadly the stack beneath onApplicationEvent(ApplicationEvent) can't handle the second call. It tries to register the beans a second time. Since the paths are the same RootSegment.addPath(String[], int, ResourceInvoker) an exception is thrown saying "You cannot have 2 locators for same path: ***" (stack trace below).

      I think listing to the refresh event is a good idea, but RESTeasy should handle multiple refresh events correct. Recognize changed configurations would be nice, but ignoring present resources would be okay for me too.

      Exception Stack Trace
      Context initialization failed
      org.jboss.resteasy.spi.LoggableFailure: You cannot have 2 locators for same path: {userId}/history
              at org.jboss.resteasy.core.registry.RootSegment.addPath(RootSegment.java:67)
              at org.jboss.resteasy.core.registry.RootSegment.addPath(RootSegment.java:97)
              at org.jboss.resteasy.core.registry.RootSegment.addPath(RootSegment.java:258)
              at org.jboss.resteasy.core.ResourceMethodRegistry.processMethod(ResourceMethodRegistry.java:219)
              at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:124)
              at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:106)
              at org.jboss.resteasy.plugins.spring.SpringBeanProcessor.onApplicationEvent(SpringBeanProcessor.java:413)
              at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97)
              at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:303)
              at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:911)
              at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:428)
      

      To reproduce the problem just register this class additionally to a JAX-RS annotated with Spring:

      TriggerRefresh.java:java
      import org.springframework.beans.BeansException;
      import org.springframework.beans.factory.InitializingBean;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.ApplicationContextAware;
      import org.springframework.web.context.ConfigurableWebApplicationContext;
      
      public class TriggerRefresh implements ApplicationContextAware, InitializingBean {
      
          private ConfigurableWebApplicationContext context;
      
          @Override
          public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
              context = (ConfigurableWebApplicationContext)applicationContext;
          }
      
          @Override
          public void afterPropertiesSet() throws BeansException {
      	//Change the context somehow e.g. setConfigLocations()
      	context.refresh();
          }
      }
      

        1. NewContextRefreshTest.java
          2 kB
          Ronald Sigal

              rsigal@redhat.com Ronald Sigal
              NvtMorch Holger Morch (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: