我们的目标是在测试环境下非Cxf开发时启动不进行Cxf环境的加载, 正式环境或Cxf开发时启动即进行Cxf环境的装载.
1. 概述
- 主要是利用了servlet3.0规范里新增的
ServletContainerInitializer
接口. - 当然我们不会直接使用该接口, 而是退而求次地借助于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. 关键思路
<context-param />
,Listener
,ServletContainerInitializer
的生效时机的先后顺序.- Tomcat容器对添加
<context-param />
的处理逻辑.