Jersey+Spring+MyBatis 自动装配似乎有问题

发布于 2024-12-25 13:19:41 字数 5296 浏览 2 评论 0原文

因此,我有一个项目正在从 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>

,然后我的 Rest 类具有以下内容......

@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。

有人有什么想法吗?

So I have a project I am converting over from Spring MVC to Jersey REST, persistence is currently handled by MyBatis due to client requirements. Everything looks fine I am using the following is my web.xml and 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>

Snippets from application Context

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

And then my Rest class has the following....

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

However when I run everything daoImpl is null.

Anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇见了你 2025-01-01 13:19:41

我遇到同样的问题。您可以通过简单的方法修复它。

将 @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);
    }

}

I encounter the same issue. You can fix it by a easy way.

Add @Autowire to your rest class, such as,

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文