环境切换的目的是 准生产和生产环境切换时,只修改一个文件就可以达到效果
在spring bean 文件中 配置:
<bean class="cn.zno.common.context.GServletContext"></bean>
package cn.zno.common.context; import java.util.Date; import javax.servlet.ServletContext; import org.springframework.web.context.ServletContextAware; public class GServletContext implements ServletContextAware { private ServletContext servletContext; @Override public void setServletContext(ServletContext servletContext) { this.servletContext=servletContext; getServletContext().setAttribute("resouceVersion", new Date().getTime()); getServletContext().setAttribute("swaggerPath", "http://localhost:8080/cxf-rs-swagger"); } public ServletContext getServletContext() { return servletContext; } }
在index.jsp 中使用
url = "${swaggerPath}/api/swagger.json";
再进一步:
把"http://localhost:8080/cxf-rs" 值配置到 properties 文件中
swagger.path=http://localhost:8080/cxf-rs-swagger
spring bean 中
<util:properties id="config" location="classpath:conf/config.properties"></util:properties>
常量文件
package cn.zno.common.constants; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ApplicationConstants { public static String SWAGGER_PATH; @Autowired(required = true) public void setSWAGGER_PATH(@Value("#{config['swagger.path']}") String SWAGGER_PATH) { ApplicationConstants.SWAGGER_PATH = SWAGGER_PATH; } }
使用该常量
getServletContext().setAttribute("swaggerPath", ApplicationConstants.SWAGGER_PATH);
完整项目:
git@github.com:witaste/cxf-rs-swagger.git