springboot配置webservice服务发布

1、自动配置类:

@Configuration
public class AutoConfiguration {
@Bean
    public ServletRegistrationBean registrationAxisServlet(AxisServlet axisServlet) {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(axisServlet, "/services/*");
        // servletRegistrationBean.setLoadOnStartup(100);//默认为-1访问时才初始化
        return servletRegistrationBean;
    }

    @SuppressWarnings("rawtypes")
    @Bean
    public ServletListenerRegistrationBean listenerRegistrationAxisServlet() {
        return new ServletListenerRegistrationBean<ConfigAxisServletContextListener>(new ConfigAxisServletContextListener());
    }

    @Bean
    public AxisServlet axisServlet() {
        return new AxisServlet();
    }
}

2、配置路径:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConfigAxisServletContextListener implements ServletContextListener {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        
        logger.info("测试信息,进入处理server-config.wsdd的JAVA类");

        String wsdd = "server-config.wsdd"; 

        File target = new File(sce.getServletContext().getRealPath("/WEB-INF") + File.separator + wsdd);
        logger.info("server-config.wsdd 的路径为:",target.getAbsolutePath());
        if (!target.exists()) {
            try {
                target.getParentFile().mkdirs();
                target.createNewFile();
            } catch (IOException e) {
                logger.debug("获取创建【/WEB-INF】路径出错:" + e.getMessage(), e);
            }

            String path = "/config/" + wsdd;
            File source = new File(path);

            try (InputStream is = new FileInputStream(source); FileOutputStream os = new FileOutputStream(target);) {
                int bytesRead = 0;
                byte[] buffer = new byte[10 * 1024];
                while ((bytesRead = is.read(buffer, 0, buffer.length)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
            } catch (IOException e) {
                logger.warn("将文件【config/" + wsdd + "】复制到【/WEB-INF】下出错:" + e.getMessage());
            }
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }

}

 

posted @ 2020-06-23 11:12  凉城  阅读(3748)  评论(0编辑  收藏  举报