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注解設置