生产环境屏蔽swagger(动态组装bean)
spring动态组装bean
背景介绍:
整合swagger时需要在生产环境中屏蔽掉swagger的地址,不能在生产环境使用
解决方案
使用动态profile在生产环境中不注入swagger的bean
swagger配置 profile="dev"
<beans profile="dev">
<bean
class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration"
id="swagger2Config" />
<mvc:resources location="classpath:/META-INF/resources/"
mapping="swagger-ui.html" />
<mvc:resources
location="classpath:/META-INF/resources/webjars/"
mapping="/webjars/**" />
</beans>
web.xml中上下文配置
<!-- spring中活动的profiles
主要用来加载swagger
开发环境设置为<u>dev</u>加载swagger的bean
生产环境设置问<u>prod</u>不加载
-->
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value><u>dev</u></param-value>
</context-param>
本文采取的是作为web应用的上下文参数
注:还有多种配置方式:
- 作为DispatcherServlet的初始化参数
- 作为Web应用的上下文参数
- 作为JNDI条目
- 作为环境变量
- 作为JVM的系统属性
- 在继承测试上,使用@ActiveProfiles注解设置