Spring Boot(十三)補充講解 @SpringBootApplication banner 獲取配置項

 1.@SpringBootApplication 幾個常用屬性


    1.scanBasePackages


        默認掃描的包的路徑是當前包以及子包下的所有類,可以通過scanBasePackages修改掃描包路徑
            (如:@SpringBootApplication(scanBasePackages="com.edu.spring.springboot.bean"))

    2.排除指定的類/配置類


        1.exclude :根據class來排除


            (如:@SpringBootApplication(exclude=Runnable.class))


        2.excludeName:根據class全局名稱 來排除


            (如:@SpringBootApplication(excludeName="java.lang.Runnable"))

 2.banner


    1.SpringApplication.setBannerMode()


        1.SpringApplication.setBannerMode(Banner.Mode.OFF)  :關閉banner的輸出


        2.其他幾個Mode

 

    enum Mode {

        /**
         * Disable printing of the banner.
         */
        OFF,

        /**
         * Print the banner to System.out.
         */
        CONSOLE,

        /**
         * Print the banner to the log file.
         */
        LOG

    }


    2.自定義banner的方法


        1.文字的banner


            1.在classpath下放一個banner.txt文件即可
            2.使用 banner.location 配置項來指定文字banner的文件路徑及名稱

 

        2.圖片的banner


            1.springboot支持圖片的banner,圖片格式支持jpg/png/gif
            2.使用 banner.image.location 配置項來指定圖片banner的文件路徑及名稱

        3.使用 banner.charset 配置項來指定 banner的編碼,默認UTF-8

 

 3.獲取配置項的值的3種方法


    1.ConfigurableApplicationContext.getEnvironment().getProperty("key")


    2.編寫一個獲取 配置項的類,創建 屬性 在屬性上 加@Value("${key}") 註解。key等於 要得到的配置項的key


    3.SpringApplication.setDefaultProperties(Map<String, Object> defaultProperties) 或
      SpringApplication.setDefaultProperties(Properties defaultProperties)

    4.獲取配置項的值優先級:


        1.application.properties 文件裏的配置
        2.SpringApplication.setDefaultProperties()的配置
        3.@Value("${key}") 註解 或 ConfigurableApplicationContext.getEnvironment().getProperty("key")的配置

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章