0

I'm using Jersey + Spring and I have a Mapper interface whose implementation is in xml that contains query. The @Autowired annotation works fine in the spring controller but it's not accepting @Autowired in the Jersey and say no bean is registered for this. How can I make it work in Jersey Resource class as its working in the Spring Controller?

EmployMapper interface

package com.resource.mapper;
import java.util.ArrayList;
import org.apache.ibatis.annotations.Param;


public interface EmployMapper {
    public int addUser(@Param("name") String name);

}

EmployMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.resource.mapper.EmployMapper">
     <update id="addUser" > 
    insert INTO mytable (name) VALUES(#{name}); 
    </update>
</mapper>

Spring Controller

package com.controller;

@Controller
public class EmployController {
    @Autowired
    EmployMapper employMapper;

    @RequestMapping("test")
    public ModelAndView reviewspage()
    {
        employMapper.addUser("SpringMVC");
          ModelAndView modelAndView = new ModelAndView("test");         
            return modelAndView;
    }
}

Jersey Resource

package com.resource;

@Component
@Path("/e")
public class MyResource {
    @Autowired
    EmployMapper mapper;
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getEmployees(){
        mapper.addUser("Done it");
        return "Biggest succcess of the year";
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" 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_3_0.xsd"
    metadata-complete="false">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <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.resource</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>


</web-app>

applicationContext

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.resource" />
    <context:annotation-config />
    <bean id="employMapper" class="com.resource.mapper.Example" />



     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/employtrackingsystem"/>
    <property name="username" value="adminXuMPZTn" />
    <property name="password" value="IMhzT-AJE47P" />
  </bean>


   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />
     <property name="mapperLocations" value="classpath*:*resource/mapper/EmployMapper.xml"/>

</bean>


  <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
      <constructor-arg index="0" ref="sqlSessionFactory" />
 </bean>

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.resource.mapper"/>
</bean>

</beans>

Here @Autowired is not working and say bean didn't find any match

Error

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myResource': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.resource.mapper.EmployMapper com.resource.MyResource.mapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.resource.mapper.EmployMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Awais Ahmad
  • 427
  • 3
  • 17

1 Answers1

-2

try this solution to enable Jersey to use Spring beans

Community
  • 1
  • 1
MrFylypenko
  • 116
  • 1
  • 6
  • Incorrect. He already has the pre-requisite `` – rmlan Aug 26 '16 at 15:22
  • org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 26; The prefix "mvc" for element "mvc:annotation-driven" is not bound. – Awais Ahmad Aug 26 '16 at 15:29
  • 1
    try this [solution](http://stackoverflow.com/a/25900580/6752984) and show pom.xml dependencies – MrFylypenko Aug 26 '16 at 17:13