Cxf——动态加载

我们的目标是在测试环境下非Cxf开发时启动不进行Cxf环境的加载, 正式环境或Cxf开发时启动即进行Cxf环境的装载.

1. 概述

  1. 主要是利用了servlet3.0规范里新增的ServletContainerInitializer接口.
  2. 当然我们不会直接使用该接口, 而是退而求次地借助于spring-web中的SpringServletContainerInitializer中声明的WebApplicationInitializer接口.

2. 代码

// 居于 AbstractHttpSessionApplicationInitializer执行时机之后
@Order(101)
public class CxfInitializer implements WebApplicationInitializer {

    // 动态加载Cxf, 加快测试环境下启动速度

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        String startImmediately = (String) PropertiesUtil.getProperty("cxf.startImmediately");
        if (!("true".equalsIgnoreCase(startImmediately))) {
            return;
        }

        injectContextParam(servletContext);

        registerServlet(servletContext);
    }

    @SuppressWarnings("unchecked")
    private void injectContextParam(ServletContext servletContext) {
        // ContextLoaderListener的执行时机比这个方法执行的时机要晚, 所以我们在这里将spring-cxf-service.xml配置文件加入<context-param>是来得及的
        // 不过这里有个细节就是在Tomcat对servletContext.setInitParameter的实现中使用的是ConcurrentMap<String, String>.putIfAbsent来完成的. 所以我们必须采取先移除, 在插入的操作.
        // 感觉最近的几个需求都得剑走偏锋.

        /*
            <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                    classpath:config/spring.xml;
                    <!-- cxf配置 -->
                    classpath:config/spring-cxf-service.xml;
                </param-value>
            </context-param>
        */

        String contextConfigLocationInitParameter = StringUtils.EMPTY;

        // 所以以下是依赖于Servlet容器的
        ConcurrentMap<String, String> parameters = (ConcurrentMap<String, String>) SystemMetaObject
                .forObject(servletContext).getValue("context.parameters");

        // 确保插入 spring-cxf-service.xml 配置文件
        if (parameters.containsKey("contextConfigLocation")) {
            contextConfigLocationInitParameter = servletContext.getInitParameter("contextConfigLocation");
            if (!contextConfigLocationInitParameter.contains("spring-cxf-service.xml")) {
                contextConfigLocationInitParameter += "classpath:config/spring-cxf-service.xml;";
            }

            parameters.remove("contextConfigLocation");
        }else{
            contextConfigLocationInitParameter += "classpath:config/spring-cxf-service.xml;";
        }

        servletContext.setInitParameter("contextConfigLocation", contextConfigLocationInitParameter);
    }

    private void registerServlet(ServletContext servletContext) {

        /*
            <!-- cxf servlet -->
            <servlet>
                <servlet-name>CXFServlet</servlet-name>
                <servlet-class>
                    org.apache.cxf.transport.servlet.CXFServlet
                </servlet-class>
                <load-on-startup>1</load-on-startup>
            </servlet>
            <servlet-mapping>
                <servlet-name>CXFServlet</servlet-name>
                <url-pattern>/webservice/*</url-pattern>
            </servlet-mapping> 
         */

        javax.servlet.ServletRegistration.Dynamic registration = servletContext.addServlet(
                "CXFServlet", org.apache.cxf.transport.servlet.CXFServlet.class);
        registration.setLoadOnStartup(1);
        registration.addMapping("/webservice/*");
    }
}

3. 关键思路

  1. <context-param /> , Listener, ServletContainerInitializer的生效时机的先后顺序.
  2. Tomcat容器对添加<context-param />的处理逻辑.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值