Question

There are lots of question have been asked around this question, but I am still not getting the root cause of what is preventing my bean to be autowired.
I have following level of packages:

com.pack.amg.service  
com.pack.amg.repository
com.pack.amg.beans  
com.pack.amg.resources  

applicationContext.xml (which is required by ContextLoaderListner in web.xml)

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-3.2.xsd
                    http://www.springframework.org/schema/data/neo4j
                    http://www.springframework.org/schema/data/neo4j/spring-neo4j-3.0.xsd
                    http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


<context:component-scan base-package="com.pack.amg" />

<bean id="graphDatabaseService" class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
    <constructor-arg value="http://localhost:7474/db/data/" index="0" />
</bean>
<neo4j:config graphDatabaseService="graphDatabaseService"
    base-package="com.pack.amg.repository" />
<neo4j:repositories base-package="com.pack.amg.repository" />
<tx:annotation-driven mode="proxy" />

I am expecting here to auto scan all of my packages and subpackages and its classes using <context:component-scan base-package="com.pack.amg" /> which may be true but is not working at all..! Following is my REST resource:
TestResource.java

@Path("/")
@Service
public class TestResource 
{   
    @Autowired
    private MyService myService;  
    @Path("/save")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response getResponse(ParamBeans bean)
    {
       System.out.println(bean);
       String str = myService.save(bean); // <-- here myService is evaluating as null !!
       return Response.ok(str).build();
    }  
}  

MyService.java

@Service
public class MyService
{
    @Autowired
    private MyRepository repository;

    public String save(ParamBeans bean)
    {
        return "Saved";
    }
}  

My war is deploying properly without any Exception, but while calling my REST service, it evaluating MyService as null. does anyone know why ? Thanks

Was it helpful?

Solution

M.Deinum is correct. You must need to make your Spring aware of your jersey. I had the same issue before, You need to add additional dependency and configuration in pom.xml as below:

<dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-spring</artifactId>
        <version>1.18.1</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </exclusion>
        </exclusions>
    </dependency>  

and replace your old implementation of Jersey servlet from web.xml with following one:

<servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.your.package.resources</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>  
<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern> <!-- depending upon your choice -->
</servlet-mapping>  

Where com.sun.jersey.spi.spring.container.servlet.SpringServlet belongs to latest dependency added i.e. jersey-spring. Hope this helps

Update

As suggested by cronemberger, there seems some issues with Jersey 2 lib. So it may happen above implementation may not work with Jersey 2, but I need to check with the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top