如何在WAR应用程序中自动连接Web服务客户端(使用Spring)



我使用的是Maven 3.0.3、CXF 2.7.15和Spring 3.2.11.RELEASE。使用JAX-WS Maven插件,可以自动生成web服务客户端类(插件代码如下)。。。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>wsimport</goal>
            </goals>
            <configuration>
                <wsdlDirectory>${basedir}/src/wsdl</wsdlDirectory>
                <sourceDestDir>${basedir}/src/main/java</sourceDestDir>
                <packageName>org.collegeboard.bsorg</packageName>
            </configuration>
        </execution>
    </executions>
 </plugin>

包括以下

package org.myproject.bsorg;
/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.1.3-b02-
 * Generated source version: 2.1
 * 
 */
@WebService(name = "OrganizationWebService", targetNamespace = "http://mainco.org/bsorg/")
@XmlSeeAlso({
    ObjectFactory.class
})
public interface OrganizationWebService {

然后我有了自己的服务类,在其中我尝试通过自动布线参考上述内容…

package org.mainco.subco.myproject.service;
@Service("orgWsdlSvc")
public class OrgWsdlServiceImpl implements OrgWsdlService 
{
    @Autowired
    private OrganizationWebService m_ows;

当我部署我的WAR文件时,我得到错误

09:20:17,846 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-14) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orgWsdlSvc': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.mainco.bsorg.OrganizationWebService org.mainco.subco.myproject.service.OrgWsdlServiceImpl.m_ows; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.mainco.bsorg.OrganizationWebService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:290) [spring-beans-3.2.11.RELEASE.jar:3.2.11.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1148) [spring-beans-3.2.11.RELEASE.jar:3.2.11.RELEASE]

但我对如何正确地自动连接web.xml和附带的上下文文件感到困惑。在我的WEB-INF/WEB.xml中,我有

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:/META-INF/spring/applicationContext-myproject.xml,
        classpath:/META-INF/spring/infrastructure.xml
    </param-value>
</context-param>
<context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>myproject.webapp</param-value>
</context-param>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

在我的WEB-INF/dispatcher-servlet.xml文件中,我有

<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<context:component-scan base-package="org.mainco" />
<!-- Define spring bean for use with this app. -->
<bean id="localPropertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:application.properties</value>
    </property>
</bean> 
<!-- Define application properties for use in Spring classes -->
<util:properties id="applicationProperties" location="classpath:application.properties" />
<tx:annotation-driven />

在我的"applicationContext myproject.xml"中,我有

<util:properties id="applicationProperties" location="classpath:application.properties" />
<util:properties id="coreProperties" location="classpath:core.properties" />
<context:component-scan base-package="org.mainco.subco" />
<context:component-scan base-package="org.mainco.bsorg" />
<bean id="sessionTimeoutInSeconds" class="java.lang.Long">
    <constructor-arg>
        <value>3600</value>
    </constructor-arg>
</bean>
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
    <!-- Define spring bean for use with this app. -->
    <bean id="localPropertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location">
                    <value>classpath:application.properties</value>
            </property>
    </bean>
    <bean id="assert" class="org.mainco.subco.util.Assert" factory-method="createInstance" />
    <http-conf:conduit name="https://.*">
            <http-conf:tlsClientParameters secureSocketProtocol="TLSv1" disableCNCheck="true">
                    <sec:trustManagers>
                        <sec:keyStore type="JKS" password="${key.store.password}" resource="${key.store.file}" />
                    </sec:trustManagers>
                    <sec:keyManagers keyPassword="${key.manager.password}">
                        <sec:keyStore type="pkcs12" password="${private.key.password}" resource="${private.key.file}" />
                    </sec:keyManagers>
            </http-conf:tlsClientParameters>
    </http-conf:conduit>
<jaxws:client id="orgWebServiceClient"
    serviceClass="org.mainco.bsorg.OrganizationWebService"
    address="${wsdl.url}"
    /> 
<bean id="organizationWebService" class="org.mainco.bsorg.OrganizationWebService"></bean>

我需要做什么才能自动连接我的web服务客户端?

编辑:根据给出的建议,我将bean定义添加到我的应用程序上下文(见上文)文件中,但在分解应用程序时收到以下错误:

Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orgWsdlSvc': Injection of autowired dependencies failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.myproject.bsorg.OrganizationWebService org.mainco.subco.myproject.service.OrgWsdlServiceImpl.m_ows; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'organizationWebService' defined in class path resource [META-INF/spring/applicationContext-myproject-mvc.xml]: Instantiation of bean failed; 
nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.mainco.bsorg.OrganizationWebService]: Specified class is an interface

您的applicationContext myproject.xml.中缺少bean定义

   <!-- Definition for OrganizationWebService bean -->
   <bean id="organizationWebService" class="org.mainco.bsorg.OrganizationWebService"></bean>

有关更多示例,请参见此:http://www.mkyong.com/spring/spring-auto-wiring-beans-with-autowired-annotation/

编辑:你正在进步。现在spring能够在应用程序上下文中找到自动连接的候选者。

然而,还有另一个问题,候选接口是一个接口,spring不能实例化它(javac也不能)。因此,您需要创建一个类来实现您的接口,并在应用程序上下文中描述它(而不是描述接口)。所以您需要创建一个实现org.mainco.bsorg.OrganizationWebServiceImp的org.mainco-bsorg.OrganizationWebService类,并将应用程序上下文更改为:

   <bean id="organizationWebService" class="org.mainco.bsorg.OrganizationWebServiceImp"></bean>

此处的更多信息:无法实例化bean类:指定的类是接口

第2版:看看https://jax-ws.java.net/2.2.1/docs/UsersGuide.html#3.1.2_Starting_from_a_WSDL_File

文件阐明:

  1. 生成服务端点接口。

  2. 实现服务端点接口。

  3. 创建一个要部署的WAR文件。

您需要用一个具体的类来实现服务端点接口,例如:OrganizationWebServiceImp

最新更新