spring boot项目中,webservice生成客户端,wsdl可配置

问题:

生成的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注入。
然后就可从配置中心获取值了。

posted @   guoruiak  阅读(6630)  评论(4编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
历史上的今天:
2017-02-20 linux 刷新环境变量
点击右上角即可分享
微信分享提示