生產環境屏蔽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注解設置


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM