如何在WAR应用程序中使用Spring自动装配我的Web服务客户端?

时间:2015-02-20 15:26:58

标签: spring cxf autowired

我正在使用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>

我需要做什么才能自动装配我的网络服务客户端?

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

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

1 个答案:

答案 0 :(得分:0)

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

此处有更多信息:Could not instantiate bean class: Specified class is an interface

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

文件澄清了:

  1. 生成服务端点接口。

  2. 实施服务端点接口。

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

  4. 您需要使用具体类实现服务端点接口,例如:OrganizationWebServiceImp