如何@autowire一个实现在xml文件中的接口(Jersey + Spring)

时间:2016-08-26 15:01:54

标签: java spring spring-mvc jersey

我使用的是Jersey + Spring,我有一个Mapper接口,其实现在xml中,包含查询。 @Autowired注释在spring控制器中工作正常但它不接受Jersey中的@Autowired并且说没有为此注册bean。如何在Jersey Resource类中使它在Spring Controller中工作?

EmployMapper界面

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;
    }
}

泽西岛资源

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>

此处@Autowired无法使用,并说豆没有找到任何匹配

错误

org.springframework.beans.factory.BeanCreationException:使用名称&#39; myResource&#39;创建bean时出错:注册自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:com.resource.mapper.EmployMapper com.resource.MyResource.mapper;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[com.resource.mapper.EmployMapper]的匹配bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

1 个答案:

答案 0 :(得分:-2)

尝试此solution以使Jersey能够使用Spring bean