【问题标题】:Create webservice with configurable endpoint via Spring通过 Spring 创建具有可配置端点的 Web 服务
【发布时间】:2014-10-18 01:31:34
【问题描述】:

我有一个应用程序正在尝试使用通过 wsdl2java 生成的类来访问 Web 服务。我希望能够对其进行配置,以便可以根据环境(TEST/PROD)使用不同的端点。

我发现以下答案正是我想要的 https://stackoverflow.com/a/3569291/346666

但是,我想使用 Spring 将服务实例注入我的服务层 - 是否有纯 Spring 方法来处理上述问题?

或者,有没有更好的方法将 web 服务的实例注入到一个类中,并且仍然能够动态配置端点?

【问题讨论】:

    标签: java spring web-services


    【解决方案1】:

    使用 Spring 基于 Java 的配置:

    @Configuration
    public class HelloServiceConfig {
    
        @Bean
        @Scope("prototype")
        public HelloService helloService(@Value("${webservice.endpoint.address}") String endpointAddress) {
            HelloService service = new HelloService();
            Hello port = service.getHelloPort();
            BindingProvider bindingProvider = (BindingProvider) port;
            bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,endpointAddress);
            return service;
        }
    
    }
    
    @Component
    public class BusinessService {
    
         @Autowired
         private HelloService hellowService;
         ...
    
         public void setHelloService(HelloService helloService) {
            this.helloService = hellowService;
         }
    }
    

    编辑

    要将其与 Spring 基于 XML 的配置一起使用,您只需将 HelloServiceConfig 注册为 Spring 上下文 xml 文件中的 bean:

    <bean class="com.service.HelloServiceConfig.class"/>
    
    <bean id="businessService" class="com.service.BusinessService">
         <property name="helloService" ref="helloService"/>
    </bean>
    

    在 Spring 中创建 Web 服务客户端的其他替代方法包括使用 Spring Web ServicesApache CXF。这两个选项都允许基于 wsdl2java 仅使用 XML 定义 JAX-WS 客户端,但需要额外的依赖项。

    【讨论】:

    • 我应该澄清一下。我正在寻找一个没有自动装配的基于 Spring XML 的解决方案。你知道这样做的方法吗?
    • 我最终使用 Apache CXF 使用纯 Spring-XML 方法:cxf.apache.org/docs/… - 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多