users@jersey.java.net

[Jersey] Application.getSingletons() works not as expected

From: <gsl_at_gslweb.de>
Date: Thu, 28 Apr 2011 20:58:21 +0000 (GMT)

Hello,

I am writing a webapp using Jersey 1.6 together with Spring
and am trying to integrate both. I am getting

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at
com.sun.jersey.core.spi.component.ComponentInjector.setMethodValue(Comp
onentInjector.java:168)
        at
com.sun.jersey.core.spi.component.ComponentInjector.inject(ComponentInj
ector.java:133)
        at
com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(Root
ResourceUriRules.java:146)
        at
com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(Web
ApplicationImpl.java:1178)
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.ja
va:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccesso
rImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at
com.sun.jersey.core.spi.component.ComponentInjector.setMethodValue(Comp
onentInjector.java:166)
        ... 29 more
Caused by: java.lang.IllegalStateException
        at
com.sun.jersey.server.impl.ThreadLocalHttpContext.getUriInfo(ThreadLoca
lHttpContext.java:74)
        at
com.sun.jersey.server.impl.application.WebApplicationImpl$2.invoke(WebA
pplicationImpl.java:269)
        at $Proxy5.getRequestUri(Unknown Source)
        at
my.package.MyResourceImpl.setServletContext(MyResourceImpl.java:108)

I have seen the integration using the SpringServlet (from the
jersey-spring package), but I am trying a different approach, the
one shown in Bill Burke's book:

It works by creating the root resource class via Spring the
getSingletons() method of a custom subclass of
javax.ws.rs.core.Application:

    @Override
    public Set<!-- ct> getSingletons() {

        Set<!-- ct> singletons = new HashSet<!-- ct>();

        springBeansFilePath = "spring-beans.xml";

        ApplicationContext springContext = new
ClassPathXmlApplicationContext(
                    springBeansFilePath );
        Object resourceBean = springContext.getBean( "MyResource" );

        singletons.add( resourceBean );

        return singletons;
    }

The spring beans xml file excerpt defining the resource bean looks
as follows:

  <bean id="MyResource" class="my.package.MyResourceImpl">
    <property name="someProperty" ref="someOtherBean" />
  </bean>


The web.xml is:

<web-app>

  <servlet>
    <servlet-name>JAX-RS-Servlet-Jersey</servlet-name>
    <display-name>JAX-RS REST Servlet of Jersey</display-name>
   
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</s
ervlet-class>
    <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>my.package.MyJaxRsApplication</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>JAX-RS-Servlet-Jersey</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

</web-app>

Now everything should work fine, but: getSingletons() is invoked _two_
times instead of once.

Now the question to me is: Why is getSingletons() invoked two times?

As I understand the mechanism, getSingletons() must not be invoked
twice,
how should you otherwise be able work with singletons? (You'll never
know
inside getSingletons() whether you'd have to create the instance or
not.)

Maybe this method using spring beans creation in getSingletons() is a
wrong
approach now. I used this method with Jersey 1.1 and it worked there.

Any ideas?

Regards
Georg