2017-10-10 4 views
1

スプリング4でジャージー1.8を使用しようとしています。しかし、Springを使ってRestクラスに依存性を注入することはできません。スプリングでレスト(ジャージー)を設定する

私が試してautowired依存関係を呼び出すたびに、私はNULLを取得します。誰も私の依存関係が注入されていない理由を提案することはできますか?

のWeb.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" 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_2_5.xsd"> 

    <servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>*.html</url-pattern> 
    </servlet-mapping> 



    <servlet> 
     <servlet-name>Rest</servlet-name> 
     <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring-servlet.xml</param-value> 
     </init-param> 
     <init-param> 
      <param-name>com.sun.jersey.config.property.packages</param-name> 
      <param-value>org.portal.services</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Rest</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

春-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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="org.portal.services"></context:component-scan> 
    <context:annotation-config /> 

    <bean 
     class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> 
     <property name="location"> 
      <value>WEB-INF/dataSource.properties</value> 
     </property> 
    </bean> 
    <import resource="hibernate.cfg.xml" /> 
    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/"></property> 
     <property name="suffix" value=".jsp"></property> 
    </bean> 

    <bean id="addCountryService" class="org.portal.services.AddCountryService"> 
     <property name="addCountryProcessor" ref="addCountryProcessor"></property> 
    </bean> 

    <bean id="addCountryProcessor" class="org.portal.processors.AddCountryProcessor"> 
     <property name="hibernateTemplate" ref="hibernateTemplate" /> 
    </bean> 


</beans> 

休憩Coltroller

package org.portal.services; 

import javax.ws.rs.Consumes; 
import javax.ws.rs.POST; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 
import org.portal.dto.Country; 
import org.portal.processors.AddCountryProcessor; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 
import com.thoughtworks.xstream.XStream; 
import com.thoughtworks.xstream.io.xml.DomDriver; 

@Component 
@Path("/Country") 
public class AddCountryService { 

    @Autowired 
    private AddCountryProcessor addCountryProcessor; 

    @POST 
    @Path("/addCountry") 
    @Produces(MediaType.APPLICATION_XML) 
    @Consumes(MediaType.APPLICATION_XML) 
    public String addCountry(String newCountry) { 

     XStream xStream = new XStream(new DomDriver()); 
     xStream.alias("country", Country.class); 
     xStream.alias("id", String.class); 
     xStream.alias("name", String.class); 
     xStream.alias("region", Integer.class); 

     Country country = (Country)xStream.fromXML(newCountry); 
     System.out.println("processor is "+addCountryProcessor); 

     if(country != null){ 
      addCountryProcessor.addCountry(country); 
     } 
     return newCountry; 
    } 

    public void setAddCountryProcessor(AddCountryProcessor addCountryProcessor) { 
     this.addCountryProcessor = addCountryProcessor; 
    } 

    public AddCountryProcessor getAddCountryProcessor() { 
     return addCountryProcessor; 
    } 

} 

要求に "http://localhost:7070/HumanResourcePortal/rest/Country/addCountry" を掲載した後、フローは(addCountry」に来ています) "メソッドを使用していますが、addCountryProcessorをnullとして出力しています

+0

AddCountryProcessorクラスのインスタンス化や休止状態でエラーが発生した場合は、戦争開始時や展開時にログを確認できますか? –

+0

AddCountryProcessorクラスをインスタンス化するためのコンソールでエラーが発生していません – shivam

+0

[this](https://stackoverflow.com/q/19718187/8101556)の重複した可能性がありますので、[this](https:// stackoverflow)で解決できるはずです。 com/a/19751981/8101556)回答。お役に立てれば。サーブレットを変更したくない場合は、 'Autowired'の代わりに' @ com.sun.jersey.spi.inject.Inject'を実行してみてください。 –

答えて

0

二つの選択肢がここにありあります

  • @com.sun.jersey.spi.inject.Inject@Autowiredを交換してください。この@Inject注釈は、設定されたDIフレームワークから注入されます。

  • com.sun.jersey.spi.spring.container.servlet.SpringServletを使用すると、バネ統合でリソースを展開できます。春の統合について次のような依存関係を含める必要があります。

    <dependency> 
        <groupId>com.sun.jersey.contribs</groupId> 
        <artifactId>jersey-spring</artifactId> 
        <version>1.8</version> 
        <exclusions> 
        <exclusion> 
         <groupId>org.springframework</groupId> 
         <artifactId>spring-core</artifactId> 
        </exclusion> 
        <exclusion> 
         <groupId>org.springframework</groupId> 
         <artifactId>spring-beans</artifactId> 
        </exclusion> 
        <exclusion> 
         <groupId>org.springframework</groupId> 
         <artifactId>spring-context</artifactId> 
        </exclusion> 
        <exclusion> 
         <groupId>org.springframework</groupId> 
         <artifactId>spring-web</artifactId> 
        </exclusion> 
        </exclusions> 
    </dependency> 
    

春の依存性は、あなたの宣言1ではなくjersey-springで使用されるものを使用することをここに除外されています。

+0

もう1つのヘルプが必要です................プレースホルダが動作していません...ログからプロパティファイルがロードされていますが、プレースホルダが休止状態のファイルで動作していないことがわかります – shivam

+0

Canあなたは別の質問で詳細を投稿しますか? –

関連する問題