Jersey+Spring+MyBatis的自动布线似乎有问题



所以我有一个项目正在从Spring MVC转换为Jersey REST,由于客户端需求,持久性目前由MyBatis处理。一切看起来都很好我正在使用以下是我的web.xml和applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>testrest</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/context/applicationContext.xml
    </param-value>
</context-param>
<context-param>
    <param-name>defaultHtmlEscape</param-name>
    <param-value>false</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<filter> 
     <filter-name>encodingFilter</filter-name> 
     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
     <init-param> 
        <param-name>encoding</param-name> 
        <param-value>UTF-8</param-value> 
     </init-param> 
     <init-param> 
        <param-name>forceEncoding</param-name> 
        <param-value>true</param-value> 
     </init-param> 
  </filter> 
  <filter-mapping> 
     <filter-name>encodingFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
</filter-mapping>
<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
        <param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.anfcorp.ecommerce.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

应用程序上下文中的代码段

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context-2.5.xsd 
                    http://www.springframework.org/schema/mvc
                    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.anfcorp.ecommerce" /> 
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>jdbc/commerce</value>
        </property>
        <property name="resourceRef" value="true"></property>
</bean>
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
</bean>
<!-- ========================= DAO DEFINITIONS: IBATIS IMPLEMENTATIONS ========================= -->
<bean id="propertyItemDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="mapperInterface" value="com.anfcorp.ecommerce.textmanagement.mapper.PropertyItemMapper" />
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>

然后我的休息课有以下内容。。。。

@Autowired
private PropertyItemMapper daoImpl;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("id/{id}")
public PropertyItem getPropertyItem(@PathParam("id") Integer id){
return daoImpl.selectResourcesById(id);
}

然而,当我运行所有内容时,daoImpl都为null。

有人有什么想法吗?

我遇到了同样的问题。你可以用一种简单的方法来解决它。

将@Autowire添加到您的休息类中,例如

@Autowire 
public class Sampleclass {
    @Autowired
    private PropertyItemMapper daoImpl;
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("id/{id}")
    public PropertyItem getPropertyItem(@PathParam("id") Integer id){
        return daoImpl.selectResourcesById(id);
    }
}

最新更新