zoukankan      html  css  js  c++  java
  • 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注入。
    然后就可从配置中心获取值了。

  • 相关阅读:
    Java [Leetcode 190]Reverse Bits
    Java [Leetcode 88]CMerge Sorted Array
    Java [Leetcode 160]Intersection of Two Linked Lists
    Java [Leetcode 111]Minimum Depth of Binary Tree
    Java [Leetcode 225]Implement Stack using Queues
    D365 FO 视图Computed字段
    D365 FO最佳实践BP(五)-Display 方法缓存
    D365 FO最佳实践BP(四)-EDT未迁移
    How to change comment
    How to force to Fullscreen Form
  • 原文地址:https://www.cnblogs.com/morninglight/p/10405321.html
Copyright © 2011-2022 走看看