Spring Boot學習——Spring Boot設定檔application

來源:互聯網
上載者:User

標籤:blog   code   str   區別   files   private   poe   conf   ring   

        Spring Boot設定檔有兩種格式: application.properties 和 application.yml。兩種設定檔只需要使用一個。

        這兩種設定檔的文法有些區別,如下

                1. application.properties

                        server.port = 8080         -- tomcat 連接埠

                        server.context-path = /webName    -- URL路徑

                2. application.yml

                        server:

                                port: 8080         -- tomcat 連接埠,注意冒號後面有空格

                                context-path: /webName    -- URL路徑,注意冒號後面有空格

        一、Java類中使用配置

                1. 方法一

@value("${server.port}")private String port;

 

                2. 方法二

@Compoent@ConfigurationProperties(prefix="server")public class ServerProperties{      private String port;      private String context-path;            // set/get方法      }

    注意:使用註解 @Compoent是為了方便在其他類中使用@Autowired引用該類

        二、分環境使用設定檔

                再建立兩個設定檔 application-dev.yml(測試環境設定檔) 和 application-prod.yml(正式環境設定檔)

 

                在 application.yml 中配置如下:

spring:    profiles:        active: dev

                註: 上面的配置是使用設定檔application-dev.yml,改成 active:prod即可使用設定檔application-prod.yml

        三、java命令啟動使用配置

                java -jar ****.jar --spring.profiles.active=dev

                註:上面的配置是使用設定檔application-dev.yml,改成 --spring.profiles.active=prod即可使用設定檔application-prod.yml

Spring Boot學習——Spring Boot設定檔application

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.