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 <a href="892042158@qq.com" target="_blank">于国帅</a>
 * @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<String, Object> 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值