<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SpringBoot profiles

          共 3883字,需瀏覽 8分鐘

           ·

          2021-01-17 09:19

          點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”

          優(yōu)質(zhì)文章,第一時(shí)間送達(dá)

          ? 作者?|? 硬石頭

          來源 |? urlify.cn/uQbQFr

          76套java從入門到精通實(shí)戰(zhàn)課程分享

          SpringBoot 提供了profiles,可以實(shí)現(xiàn)在不同環(huán)境下應(yīng)用程序配置的隔離性。

          ?

          application.yaml

          spring:
          ??profiles:
          ????active:?prod
          application:
          ??appId:?app01??

          application-dev.yaml

          env:
          ??name:?'this?is?dev'

          application-test.yaml

          env:
          ??name:?'this?is?test'


          application-prod.yaml

          env:
          ??name:?'this?is?prod'


          Runner.java

          package?com.stone.springboot.study;

          import?org.springframework.beans.factory.annotation.Autowired;
          import?org.springframework.boot.ApplicationArguments;
          import?org.springframework.boot.ApplicationRunner;
          import?org.springframework.core.env.Environment;
          import?org.springframework.stereotype.Component;
          import?org.springframework.util.StringUtils;

          @Component
          public?class?Runner?implements?ApplicationRunner?{
          ????
          ????@Autowired
          ????Environment?environment;
          ????
          ????@Override
          ????public?void?run(ApplicationArguments?args)?throws?Exception?{
          ????????String[]?activeProfiles?=?environment.getActiveProfiles();
          ????????System.out.println("actvie?profiles->"+?StringUtils.arrayToDelimitedString(activeProfiles,?","));
          ????????String?property?=?environment.getProperty("env.name");
          ????????System.out.println("env.name->"+property);
          ????????
          ????????String?property1?=?environment.getProperty("application.appId");
          ????????System.out.println("application.appId->"+property1);
          ????}

          }

          ?

          輸出結(jié)果:

          actvie?profiles->prod
          env.name->this?is?prod
          application.appId->app01


          方式一:application.yaml中 定義spring.profiles.active

          修改application.yaml

          spring:
          ??profiles:
          ????active:?test
          application:
          ??appId:?app01

          輸出結(jié)果:

          actvie?profiles->test
          env.name->this?is?test
          application.appId->app01


          方式二:命令行參數(shù) --spring.profiles.active=prod

          輸出如下:

          actvie?profiles->prod
          env.name->this?is?prod
          application.appId->app01

          說明:命令行屬性優(yōu)先級(jí)高與application.yaml中定義的屬性

          ?

          方式三:編碼形式? SpringApplication.setAdditionalProfiles();

          ?

          Application.java
          package?com.stone.springboot.study;

          import?org.springframework.boot.SpringApplication;
          import?org.springframework.boot.autoconfigure.SpringBootApplication;

          @SpringBootApplication
          public?class?Application?{

          ????public?static?void?main(String[]?args)?{
          ????????SpringApplication?app?=?new?SpringApplication(Application.class);
          ????????app.setAdditionalProfiles("my");
          ????????app.run(args);
          ????}
          }

          ?

          application-my.yaml

          my:
          ??name:?'9wan'

          ?

          Runner.java

          package?com.stone.springboot.study;

          import?org.springframework.beans.factory.annotation.Autowired;
          import?org.springframework.boot.ApplicationArguments;
          import?org.springframework.boot.ApplicationRunner;
          import?org.springframework.core.env.Environment;
          import?org.springframework.stereotype.Component;
          import?org.springframework.util.StringUtils;

          @Component
          public?class?Runner?implements?ApplicationRunner?{
          ????
          ????@Autowired
          ????Environment?environment;
          ????
          ????@Override
          ????public?void?run(ApplicationArguments?args)?throws?Exception?{
          ????????String[]?activeProfiles?=?environment.getActiveProfiles();
          ????????System.out.println("actvie?profiles->"+?StringUtils.arrayToDelimitedString(activeProfiles,?","));
          ????????String?property?=?environment.getProperty("env.name");
          ????????System.out.println("env.name->"+property);
          ????????
          ????????String?property1?=?environment.getProperty("application.appId");
          ????????System.out.println("application.appId->"+property1);
          ????????
          ????????String?property2?=?environment.getProperty("my.name");
          ????????System.out.println("my.name->"+property2);
          ????}

          }

          輸出結(jié)果:

          actvie?profiles->my,prod
          env.name->this?is?prod
          application.appId->app01
          my.name->9wan

          說明:SpringApplication.setAdditionalProfiles("my") 添加的profile會(huì)自動(dòng)設(shè)置為激活狀態(tài)并加載

          ?

          ?

          總結(jié)

          SpringBoot 提供profiles可進(jìn)行多環(huán)境配置,再通過spring.profiles.actvife來激活特定配置。在多環(huán)境下節(jié)省了配置修改的成本。方式一可以把不同環(huán)境下相同的屬性配置放到application.yaml,避免了重復(fù)定義;? 個(gè)人也更推薦這種配置方式。

          關(guān)于application-{profile}.yaml或properties文件命名,這個(gè)是SpringBoot約定的這種格式。當(dāng)沒有激活的配置時(shí),默認(rèn)會(huì)加載application-default文件






          粉絲福利:Java從入門到入土學(xué)習(xí)路線圖

          ??????

          ??長(zhǎng)按上方微信二維碼?2 秒


          感謝點(diǎn)贊支持下哈?

          瀏覽 23
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  北条麻妃无码视频在线观看 | 裸身被操网站豆花视频网站 | 激情一二三 | 97香蕉视频| 中文字幕五月天 |