users@jersey.java.net

Re: [Jersey] Help Using _at_Inject

From: cgswtsu78 <cgray_at_proofpoint.com>
Date: Mon, 11 Jan 2010 07:26:43 -0800 (PST)

Hi Paul,

Thanks for the quick reply and suggestions. I tried using the 1.1.5 version
of the jersey-spring jar along with spring 2.5 and the behavior is still the
same. The spring bean and jersey resource both get instantiated when the
container starts but when I perform a request against the resource they are
instantiated again. So instead of a singleton instance existing, an
instance is created per resource request. The configuration (spring-jersey
jar addition) was what you suggested correct?

Paul Sandoz wrote:
>
> Hi,
>
> You need to use SpringServlet instead of ServletContainer to make
> Jersey Spring-aware:
>
> https://jersey.dev.java.net/nonav/apidocs/latest/contribs/jersey-
> spring/com/sun/jersey/spi/spring/container/servlet/SpringServlet.html
>
> (there is another way of explicitly registering an instance of
> SpringComponentProviderFactory as part of the Application/
> ResourceConfig).
>
> Then @Inject should work, but is only guaranteed to work for Spring
> 2.x. I have not tried Spring 3.0.
>
> If things do not work you might be able to look at the source and copy/
> modify appropriately for your needs:
>
> http://fisheye4.atlassian.com/browse/jersey/trunk/jersey/contribs/spring/src/main/java/com/sun/jersey/spi/spring/container/servlet/SpringServlet.java?r=HEAD
>
> http://fisheye4.atlassian.com/browse/jersey/trunk/jersey/contribs/spring/src/main/java/com/sun/jersey/spi/spring/container/SpringComponentProviderFactory.java?r=HEAD
>
> Paul.
>
> On Jan 10, 2010, at 7:23 PM, cgswtsu78 wrote:
>
>>
>> Hello,
>>
>> I have a spring singleton bean that gets loaded by the spring
>> framework via
>> the applicationcontext.xml. As the server starts up I can see the
>> constructor of the bean getting executed as expected. I then have a
>> jersey
>> resource that uses the @Inject annotation in an attempt to inject the
>> singleton spring bean instance into my jersey resource class but the
>> @Inject
>> annotation is actually recreating the bean instance as I see the bean
>> instances constructor getting called again, which isn't desired
>> since I want
>> this class to be a singleton. Am I implementing this incorrectly or
>> maybe
>> I'm misunderstanding the purpose of the @Inject. Below is my
>> configuration,
>> I'm using jersey1.1.5 and spring 3.0. Thanks for the help.
>>
>>
>> web.xml
>> <?xml version="1.0"?>
>> <web-app version="2.5"
>> xmlns="http://java.sun.com/xml/ns/javaee"
>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
>> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
>> <context-param>
>> <param-name>contextConfigLocation</param-name>
>> <param-value>WEB-INF/beans.xml</param-value>
>> </context-param>
>> <listener>
>>
>> <listener-
>> class>org.springframework.web.context.ContextLoaderListener</
>> listener-class>
>> </listener>
>> <servlet>
>> <servlet-name>Jersey Web Application</servlet-name>
>>
>> <servlet-
>> class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-
>> class>
>> <init-param>
>> <param-name>com.sun.jersey.config.property.packages</param-
>> name>
>> <param-value>com.resources</param-value>
>> </init-param>
>> <init-param>
>>
>> <param-name>com.sun.jersey.config.property.MediaTypeMappings</param-
>> name>
>> <param-value>json : application/json, xml : application/xml,
>> stream
>> : image/png</param-value>
>> </init-param>
>> <load-on-startup>1</load-on-startup>
>> </servlet>
>> <servlet-mapping>
>> <servlet-name>Jersey Web Application</servlet-name>
>> <url-pattern>/jersey/*</url-pattern>
>> </servlet-mapping>
>> </web-app>
>>
>> beans.xml:
>> <?xml version="1.0" encoding="UTF-8"?>
>> <beans xmlns="http://www.springframework.org/schema/beans"
>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xsi:schemaLocation="
>> http://www.springframework.org/schema/beans
>> http://www.springframework.org/schema/beans/spring-beans.xsd">
>> <bean id="testBean" class="com.test.TestBean"/>
>> </beans>
>>
>> Jersey Resource
>> @Path("/test")
>> public class Test {
>> @Inject TestBean testBean;
>>
>> @GET
>> @Path("exec")
>> @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
>> public Response getData() throws Exception {
>> //service code here...
>>
>>
>> }
>> }
>>
>> Spring Bean
>>
>> public class TestBean {
>> public TestBean()
>> {
>> System.out.println("here in TestBean");
>> }
>> }
>> --
>> View this message in context:
>> http://n2.nabble.com/Help-Using-Inject-tp4282068p4282068.html
>> Sent from the Jersey mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>
>

-- 
View this message in context: http://n2.nabble.com/Help-Using-Inject-tp4282068p4285933.html
Sent from the Jersey mailing list archive at Nabble.com.