users@jersey.java.net

[Jersey] Help Using _at_Inject

From: cgswtsu78 <cgray_at_proofpoint.com>
Date: Sun, 10 Jan 2010 10:23:53 -0800 (PST)

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.