2012-05-04 14 views
9

私はSpring(Autowired)を使って別のクラスを注入するRESTfulなサービス(JBossでCXFを使用しています)を書いています。しかし、クラスは注入されておらず、ヌルです。Spring BeanがCXF Webサービスに注入されていない、なぜですか?

Webサービスのインタフェースと(インジェクションが発生する必要があります)クラス

package com.company.project.web; 

@Path("/myws") 
public interface IMyWebService {  
    @POST 
    @Path("/doSomething")  
    @Consumes("application/json") 
    @Produces("application/json") 
    MyResponse doSomething(MyRequest myRequest) 
} 

@Service("myWebService") 
public class MyWebService implements IMyWebService {  
    @Autowired 
    private IMyCore myCore; 

    public MyResponse doSomething(MyRequest myRequest) { 
     .... 
    } 
} 

beans.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:jaxws="http://cxf.apache.org/jaxws" 
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd  
    http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <import resource="classpath:META-INF/cxf/cxf.xml" /> 
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 
    <import resource="classpath:META-INF/cxf/cxf-extension-http.xml" /> 
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> 

    <context:annotation-config /> 
    <context:component-scan base-package="com.company.project"/>  

    <jaxrs:server id="myWebService" address="/"> 
     <jaxrs:serviceBeans> 
      <bean class="com.company.project.web.MyWebService" /> 
     </jaxrs:serviceBeans> 
     <jaxrs:extensionMappings> 
      <entry key="json" value="application/json" /> 
     </jaxrs:extensionMappings> 
    </jaxrs:server> 
</beans> 

package com.company.project.biz; 

public interface IMyCore { 
    MyResponse doSomething(MyRequest myRequest); 
} 

@Component("myCore") 
public class MyCore implements IMyCore { 
    public MyResponse doSomething(MyRequest myRequest) { 
      ..... 
    } 
} 

を注入する必要があるもの私のセrviceはアクティブ(http://localhost:8080/{warname}/myws/doSomething)ですが、MyCoreインスタンスはmyCoreフィールド(MyCoreフィールド)にMyWebServiceに注入されていません。代わりに、NullPointerExceptionが

すべての入力がグーグルにわたって収集しようとしたスロー、それは常にnullで、予想通り、私のサービスが動作しません。いいえ、運がない!あなたの助けが高く評価されます。

よろしく

答えて

1

私の場合はbeans.xmlの

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/> 

でBean構成の下に追加しようと、それは

+0

私はこれを試してみましたが、それは私のために動作しません。 WebサービスはSpringによって作成されていないため、autowiredされていないようです。 –

8

は、Webサービスに法の下に追加してください。..働い:

@PostConstruct 
public void init() { 
    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); 
} 

現在のWebアプリケーションのコンテキスト(通常はContextLoaderListen er)がautowiringに使用されるため、IMyCore BeanはWebサービスのものではなくコンテキストリスナーの設定ファイルで定義する必要があります。

+0

今、あなたが提案した解決策を試すために、プロジェクトが少し変更されました。しかし、私は間違いなく、コードベースのローカルコピーでそれを試して、あなたが知っている貸しています。投稿ありがとう! –

+0

これは私のために働いた...ありがとう!!!! :D –

5

あなたはCXF WebサービスクラスでのSpring Beanを使用する場合は、CXFのXML設定ファイルに次のようにWebサービスを宣言する(例えば、バネcxf.xml)

<bean id="hello" class="demo.spring.service.HelloWorldImpl" /> 
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /> 

のDeclare分離豆WebServiceクラスを作成し、それをIDを持つエンドポイントに配置します。このようにSpring管理Beanがあり、AutoWiredアノテーションも使用できます。

以下のようにWebサービスを宣言すると、Beanは自動的に挿入されません。

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> 
<import resource="classpath:META-INF/cxf/cxf.xml"/> 
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> 
<jaxws:endpoint id="helloWorld" implementor="demo.spring.service.HelloWorldImpl" address="/HelloWorld"/> 

この場合は、あなたが必要とするか:

  • を注入バネ豆手動

  • SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);または豆に春から一つ一つを取得文脈

    ApplicationContext context = ...; // your Spring ApplicationContext HelloBean helloBean = (HelloBean) context.getBean("bean");

    私はJAX-RSでこれを試していませんが、私の意見ではアプローチは同じでなければなりません。

    From CXF official documentation.

関連する問題