2012-01-06 9 views
0

私はSpring MVCからJersey RESTに変換するプロジェクトを持っていますが、クライアント要件のために永続性は現在MyBatisによって処理されています。すべては私がアプリケーションのコンテキストからJersey + Spring + MyBatis autowiringで問題が発生していると思われます

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

そして、私の残りのクラスには、次のいを次の私の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> 

スニペットを使用しています正常に見えます...

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

誰もが考えている?

+0

、として、あなたの残りのクラスに@Autowireを追加しますか? @Autowiredアノテーションを使ってパブリック 'setPropertyItemMapper'を追加してみてください。 – Vlad

+0

どこにこれを追加すればよいですか?残りのクラスでは?そして、私は春Autowiredを使用しています、それは正しいですか、私はジャージー1を使用する必要がありますか?また、以下を追加しました。 \t @Autowired \t @Qualifier( "propertyItemDao") \t private PropertyItemMapper daoImpl; 名前がオフであったので、今でも同じことをしています – Jackie

+0

はい、セッターメソッドはRestクラスに追加される予定でした。これは、private PropertyItemMapper daoImpl;を定義するものです。また、Setterに@Autowired @Qualifier( "propertyItemDao")を追加します。 – Vlad

答えて

1

同じ問題が発生しました。あなたは簡単な方法でそれを修正することができます。

は、あなたのコードは、アプリケーション・サーバーで実行されている場合は、代わりにSecurityManagerがprivateフィールドへのアクセスを防ぐことができ、そのような

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

} 
関連する問題