1

下部のTLDR:

JBossWS-cxf ユーザーガイドによると、Web サービスの場合、web.xml には以下が含まれている必要があります。

    <web-app 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" 
    version="2.4">
        <servlet>
            <servlet-name>MyWebService</servlet-name>
            <servlet-class>com.sgb.MyWebService</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>MyWebService</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    </web-app>

Jboss は、WEB-INF ディレクトリ (cxf.xml ではなく) に jboss-cxf.xml という名前の記述子ファイルも必要とします。このファイルには、次のように jaxws:endpoint タグが含まれている必要があります。

            <beans xmlns='http://www.springframework.org/schema/beans'
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
        xmlns:beans='http://www.springframework.org/schema/beans'
        xmlns:jaxws='http://cxf.apache.org/jaxws'
        xsi:schemaLocation='http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://cxf.apache.org/jaxws >

        <bean id="MyWebService" class="com.sgb.MyWebService" />

        <jaxws:endpoint id="POJOEndpoint" implementor="#MyWebService" wsdlLocation="WEB-INF/wsdl/XYZ.wsdl" address="/warfilename">
            <jaxws:invoker>
                <bean class="org.jboss.wsf.stack.cxf.InvokerJSE" />
            </jaxws:invoker>
        </jaxws:endpoint>
    </beans>

次に、サービス実装クラスを次のように作成します。

    package com.sgb;

    @javax.jws.WebService(... ... ... )
    public class MyWebService implements IMyWebService
    {
        public CreateResponse create(CreateRequest request)
        {
            ... ... ... <-- an instance of createService is created
            return createService.serve(request)
        }
    }

ここまでは順調ですね。それは正常に動作します。

ただし、Spring のリファレンス ドキュメントによると、Web アプリケーションのアプリケーション コンテキストをインスタンス化する便利な方法は、web.xml に ContextLoaderListener を追加することです。

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

したがって、上記を web.xml に追加し、MyWebServiceクラスに@Serviceのアノテーションを付けて、パッケージがコンポーネント スキャン用に設定されていることを確認します。また、Spring Managed Bean にもなるはずです。

問題は、そうではないことです。JbossWS-CXF は MyWebService をインスタンス化しているように見えます。これは、依存関係が注入されず、nullpointer が発生するためです。

を使用してプログラムでapplicationContextを取得し、ClassPathXmlApplicationContext("/WEB-INF/applicationContext.xml") 次を使用して依存関係を注入/作成できますappContext.getBean()

しかし、代わりに注釈を使用して依存関係を直接注入/自動配線することを望んでいました。

TLDR:

私が現在持っているのはこれです。(この Bean は、Spring ではなく jboss によって作成されます):

    @javax.jws.WebService(... ... ... )
    public class MyWebService implements IMyWebService
    {
        private ApplicationContext appContext;
        public MyWebService(){
            appContext = new ClassPathXmlApplicationContext("/META-INF/spring/applicationContext-ws.xml");
        }

        public CreateResponse create(CreateRequest request)
        {
                            *** Use getBean() here to get my dependency. ***
            IXyzService createService = appContext.getBean("createService",IXyzService.class);
            return createService.serve(request)
        }
    }

私が欲しいのはこれです:

    @javax.jws.WebService(... ... ... )
    @Service    <-- <-- <-- ** This is Spring managed bean**
    public class MyWebService implements IMyWebService
    {
        @Resource <-- <-- <-- **Dependency Injected by Spring**
        IXyzService createService;

        public CreateResponse create(CreateRequest request)
        {
            return createService.serve(request)
        }
    }

これを達成するための最良の方法は何ですか???

4

1 に答える 1