java如何实现webservice中wsdlLocation访问地址的可配置化

背景:项目中调用了别的系统的webservice接口,调用成功之后发现wsdlLocation的地址是写死的,不方便修改,所以需要实现地址,包括用户名密码的可配置。项目的框架是Spring,调用webservice使用的是CXF。

实现可配置步骤:

step1:在spring的配置文件中加入如下配置

<!-- @value 配置资源文件 -->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:resources.properties</value>
</list>
</property>
</bean>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties"/>
</bean>
上面的配置可以将字符串中的"${name}",转换为name在resource.property中设置的值。
step2:使用CXF将wsdl解析为java代码,并且使用占位符"${name}"替换service文件中wsdlLocation和其他使用的地址的地方。如

@WebServiceClient(name = "XXXXXX",
wsdlLocation = "${ADDRESS}",
targetNamespace = "urn:sap-com:document:sap:soap:functions:mc-style")
public class XXXXXXX_Service extends Service {

step3:使用CXF来调用webservice,首先需要将resource.property中的值注入到bean中,然后使用CXF来调用,如下
@Value("${UserName}")
private String UserName;
@Value("${Password}")
private String PassWord;
@Value("${ADDRESS}")
private String Address;
JaxWsProxyFactoryBean bean=new JaxWsProxyFactoryBean();
bean.setAddress(Address);
bean.setServiceClass(Service_Interface.class);
bean.setUsername(UserName);
bean.setPassword(PassWord);
Service_Interface port=(Service_Interface) bean.create();//得到service。
port.method();//调用service的方法
现在代码中就可以直接使用resource.property中配置的地址和用户名密码了

---------------------
转自:https://blog.csdn.net/u012000209/article/details/55202721

原文地址:https://www.cnblogs.com/liushui-sky/p/10831374.html