webservice 搭配spring 实现自动扫描注册 配置端口

这个版本时webservice 利用spring 进行的自动扫描注册

后面提供 一个独立的webservice不依赖其他框架的自动扫描注册

1.InitWebService

package com.ld.admin.listener;

import java.util.HashMap;
import java.util.Map;

import javax.jws.WebService;
import javax.servlet.ServletContext;
import javax.xml.ws.Endpoint;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import com.ld.model.ConfigModel;

import lombok.extern.slf4j.Slf4j;

/**
 * 初始化注册所有的webService
 * 
 * @ClassName InitWebService
 * @author 于国帅
 * @date 2018年9月12日 下午4:43:58
 *
 */
@Component
@Slf4j
public class InitWebService implements InitializingBean, ServletContextAware, ApplicationContextAware {
    private static ApplicationContext applicationContext;
    private static final String WEB_SERVICE_PACKAGE = "com.ld.webservice";// 默认webservice扫描的类
    private static final String WEB_SERVICE_URL = "/webService/";// 默认webservice扫描的类
    private static final Map webServiceMap = new HashMap<>(); // webService url 路由

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        InitWebService.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        try {
            initWebService(servletContext);
        } catch (Exception e) {
            log.error("初始化webService  异常 请检查");
        }

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // Auto-generated method stub

    }

    private void initWebService(ServletContext context) {
        // 获取日志是否打印
        boolean logFlag = ConfigModel.CONFIG.getPropertyBooleanValue("log4j.out.webservice", false);
        // 获取域名配置
        String webServiceDomain = ConfigModel.CONFIG.getProperty("webServiceDomain");
        // 先扫描到所有的类
        String[] beans = InitWebService.getApplicationContext().getBeanDefinitionNames();
        for (String beanName : beans) {
            Class beanType = InitWebService.getApplicationContext().getType(beanName);
            if (beanType.getPackage().toString().contains(WEB_SERVICE_PACKAGE)) {
                // 如果处于扫描的包下,并且存在javax.jwsWebService这个注解 那么证明就是webService
                if (beanType.getAnnotation(WebService.class) != null) {
                    // 注册
                    String domainUrl = webServiceDomain + WEB_SERVICE_URL + beanName;
                    Endpoint.publish(domainUrl, InitWebService.getApplicationContext().getBean(beanName));
                    if (webServiceMap.containsKey(domainUrl)) {
                        log.error("webService路由存在重复url 暂不注册 url=" + domainUrl + "?wsdl");

                    } else {
                        webServiceMap.put(domainUrl, beanType);
                        if (logFlag) {
                            log.error("webService注册路径====" + domainUrl + "?wsdl");
                        }
                    }

                }
            }
        }
    }
}

2.配置文件

#webservice 注册接口日志是否打印
log4j.out.webservice =true
==================webService 配置==========
webservice 的域名配置 端口不可重复 http://localhost:8080 发布后 默认为 webServiceDomain/webService/下访问
webServiceDomain= http://localhost:9000

你可能感兴趣的:(【webservice】)