【问题标题】:How to make an @WebService spring aware如何使@WebService 具有弹簧意识
【发布时间】:2011-06-29 19:04:53
【问题描述】:

我有一个 Web 服务,我正在尝试将变量自动装配到其中。这是课程:

package com.xetius.isales.pr7.service;

import java.util.Arrays;
import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;

import com.xetius.isales.pr7.domain.PR7Product;
import com.xetius.isales.pr7.domain.PR7Upgrade;
import com.xetius.isales.pr7.logic.UpgradeControllerInterface;

@WebService(serviceName="ProductRulesService",
            portName="ProductRulesPort",
            endpointInterface="com.xetius.isales.pr7.service.ProductRulesWebService",
            targetNamespace="http://pr7.isales.xetius.com")
public class ProductRulesWebService implements ProductRulesWebServiceInterface {

    @Autowired
    private UpgradeControllerInterface upgradeController;

    @Override
    public List<PR7Product> getProducts() {
        if (upgradeController == null) {
            return Arrays.asList(new PR7Product("Fail"));
        }
        return upgradeController.getProducts();
    }

    @Override
    public List<PR7Upgrade> getUpgrades() {
        if (upgradeController == null) {
            return Arrays.asList(new PR7Upgrade("Fail"));
        }
        return upgradeController.getUpgrades();
    }

    @Override
    public List<PR7Product> getProductsForUpgradeWithName(String upgradeName) {
        if (upgradeController == null) {
            return Arrays.asList(new PR7Product("Fail"));
        }
        return getProductsForUpgradeWithName(upgradeName);
    }

}

但是,当我尝试访问 Web 服务时,我得到了返回的 Fail 版本,这意味着 upgradeController 没有被自动装配。这是我的应用上下文:

<?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="com.xetius.isales.pr7" />
    <context:annotation-config />

    <bean id="upgradeController" class="com.xetius.isales.pr7.logic.UpgradeController" />

</beans>

如何使@WebService 类能够感知弹簧并自动装配

【问题讨论】:

    标签: web-services spring jax-ws autowired


    【解决方案1】:

    如果你想要自动装配,ProductRulesWebService 需要扩展 SpringBeanAutowiringSupport

    扩展该类将允许 UpgradeController 自动装配

    【讨论】:

      【解决方案2】:

      使用支持Spring natively的CXF之类的堆栈,那么你基本上可以这样做:

      <bean id="aService" class="com.xetius.isales.pr7.service.ProductRulesWebService " />
      
      <jaxws:endpoint id="aServiceEndpoint" implementor="#aService" address="/aService" />
      

      【讨论】:

      【解决方案3】:

      根据容器版本甚至 Spring,在下文中您将有一个简单的解决方案来公开您的 WSDL,使用:

      @PostConstruct
      SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
      

      【讨论】:

        猜你喜欢
        • 2017-02-16
        • 2010-11-26
        • 2011-09-19
        • 1970-01-01
        • 1970-01-01
        • 2013-09-09
        • 1970-01-01
        • 2017-12-22
        • 1970-01-01
        相关资源
        最近更新 更多