问题:
生成的ws(webservice)客户端代码,wsdl的文件地址写死在文件中,无法从配置中心读取。
步骤:
查看地址写死位置
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("http://xx.xxx.xx.xxx:8001/soa-infra/services/HOL/HOL_M02_PageInquiryParentCompanyInfoSrv/HOL_M02_PageInquiryParentCompanyInfoSrv_ep?WSDL");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
HOLM02PAGEINQUIRYPARENTCOMPANYINFOSRVEP_WSDL_LOCATION = url;
HOLM02PAGEINQUIRYPARENTCOMPANYINFOSRVEP_EXCEPTION = e;
}
静态代码块中的变量注入
由于静态代码块会先于spring注入执行,故常规思路不可行。
创建配置文件schedule.properties,和配置类ScheduleHrConfig。配置填写在schedule.properties中。
@Configuration
@PropertySource("classpath:schedule.properties")
@EnableCaching
public class ScheduleHrConfig
{
@Value("${hr.sync.dept.wsdl}")
private String deptWsdl;
public String getDeptWsdl()
{
return deptWsdl;
}
public void setDeptWsdl(String deptWsdl)
{
this.deptWsdl = deptWsdl;
}
}
静态注入之前,先实现 spring 的一个获取上下文的工具类
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* Spring工具类,获取Spring上下文对象等
*/
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(SpringContextUtil.applicationContext == null){
SpringContextUtil.applicationContext = applicationContext;
}
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name){
return getApplicationContext().getBean(name);
}
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
public static <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name, clazz);
}
}
静态注入:
static {
URL url = null;
WebServiceException e = null;
try {
//url = new URL("http://soa.zte.com.cn:8001/soa-infra/services/HOL/HOL_M02_PageInquiryParentCompanyInfoSrv/HOL_M02_PageInquiryParentCompanyInfoSrv_ep?WSDL");
ScheduleHrConfig config = (ScheduleHrConfig)SpringContextUtil.getBean("scheduleHrConfig", ScheduleHrConfig.class);
url = new URL(config.getDeptWsdl());
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
HOLM02PAGEINQUIRYPARENTCOMPANYINFOSRVEP_WSDL_LOCATION = url;
HOLM02PAGEINQUIRYPARENTCOMPANYINFOSRVEP_EXCEPTION = e;
}
当然要在EP类加上@component,在调用的时候不再使用new,而是使用spring注入。
然后就可从配置中心获取值了。