users@jersey.java.net

[Jersey] Re: How to access spring context created by SpringServlet

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Thu, 20 Oct 2011 18:14:51 +0200

Hi Farrukh,

I am afraid the ApplicationContextAware/singleton combination is the best
you can get at the moment.

~Jakub

On 19.10.2011 20:54, Farrukh Najmi wrote:
>
> After much looking I have come to the conclusion that this is not a
> jersey-specific issue but is a more general Spring + Servlet issue of
> how do you get access to a spring context created by the servlet to
> container code outside the servlet.
>
> The solution I have found is not ideal as it uses static class member
> to store and retrieve the context as described here:
>
> http://mythinkpond.wordpress.com/2010/03/22/spring-application-context/
>
> If any one finds a better way please let me know. Thanks.
>
> On 10/17/2011 11:03 AM, Farrukh Najmi wrote:
>>
>> Hi Jakub,
>>
>> I have no constraints against using the ApplicationContextAware ([1])
>> interface. But I am not quite understanding how to use that interface
>> in my scenario which is as follows:
>>
>> * My main program is a junit test and creates a Spring
>> ApplicationContext using ContextConfiguration
>> <http://static.springsource.org/spring/docs/3.0.5.RELEASE/api/org/springframework/test/context/ContextConfiguration.html>
>> class annotation and AbstractJUnit4SpringContextTests
>> <http://static.springsource.org/spring/docs/3.0.5.RELEASE/api/org/springframework/test/context/junit4/AbstractJUnit4SpringContextTests.html>
>> base class
>> * A singleton bean created in that context is MyBean
>> * MyBean has a method MyBean.startServer() that starts an
>> embedded Grizzly HttpServer created using
>> com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory
>> * The Grizzly HttpServer is configured with SpringServlet
>> <http://jersey.java.net/nonav/apidocs/1.9.1/contribs/jersey-spring/com/sun/jersey/spi/spring/container/servlet/SpringServlet.html>
>> to deploy jersey server top level resource class
>> SubscriberResource into the Grizzly HttpServer.
>> * The SubscriberResource top level jersey resource class needs to
>> have its members wired using beans from my spring application
>> context created in junit test
>> * Note I do not specifically need to use SpringServlet or any
>> Servlet. All I need is for the SubscriberResource top level
>> resource class to get its members wired via spring context and
>> SpringServlet is the way I know how to do it
>>
>> I am a little fuzzy on how can I use the ApplicationContextAware
>> ([1]) interface in above scenario so jersey container uses the
>> application context created by junit test when creating the
>> SubscriberResource top level jersey resource class. As I understand
>> it the bean implementing the ApplicationContextAware must be created
>> by spring ContextFactory in order for its |*setApplicationContext
>> <http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/context/ApplicationContextAware.html#setApplicationContext%28org.springframework.context.ApplicationContext%29>*(ApplicationContext
>> <http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/context/ApplicationContext.html> applicationContext)|
>> method to be called by spring container. The problem is that jersey
>> container is responsible for creating my the SubscriberResource top
>> level jersey resource class.
>>
>> I would be grateful if you can give a little more hint on how to
>> solve this problem. Thanks.
>>
>>
>>
>> On 10/17/2011 08:03 AM, Jakub Podlesak wrote:
>>> Hi Farrukh,
>>>
>>> Any chance you could utilize Spring means to get the actual application
>>> context reference? I mean e.g. implementing ApplicationContextAware
>>> ([1]) interface?
>>>
>>> ~Jakub
>>>
>>> [1]http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/context/ApplicationContextAware.html
>>>
>>>
>>>
>>> On 13.10.2011 17:47, Farrukh Najmi wrote:
>>>>
>>>> I am working on a java class that needs to expose a jersey server
>>>> endpoint for receiving callbacks from an external process. The
>>>> class needs to be usable in a standalone java app. I am therefor
>>>> using an embedded Grizzly HttpServer created using
>>>> com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory. I am
>>>> using com.sun.jersey.spi.spring.container.servlet.SpringServlet to
>>>> deploy my spring wired jersey server into the Grizzly HttpServer.
>>>>
>>>> Is there a way to get access to spring application context created
>>>> by the SpringServlet (and beans it creates) from the container code
>>>> that creates the SpringServlet and Grizzly HttpServer after it
>>>> starts the Grizzly HttpServer?
>>>>
>>>> I see that
>>>> com.sun.jersey.spi.spring.container.servlet.SpringServlet has
>>>> protected methods |*getDefaultContext
>>>> <http://jersey.java.net/nonav/apidocs/1.9.1/contribs/jersey-spring/com/sun/jersey/spi/spring/container/servlet/SpringServlet.html#getDefaultContext%28%29>*()|and
>>>> |*getChildContext
>>>> <http://jersey.java.net/nonav/apidocs/1.9.1/contribs/jersey-spring/com/sun/jersey/spi/spring/container/servlet/SpringServlet.html#getChildContext%28java.lang.String%29>*|(java.lang.String contextConfigLocation).
>>>> Is there a reason these are protected and not public?
>>>>
>>>> Right now I would have to derive a sub-class from SprinServlet and
>>>> make these methods public. Is there a better way?
>>>> Also should I file an RFE to make these methods public?
>
> --
> Regards,
> Farrukh Najmi
>
> Web:http://www.wellfleetsoftware.com
>