玄學yml,被@ActiveProfiles注解誤導


@ActiveProfiles("')來自何處?怎么用?(不要被它的名字誤導)

ActiveProfiles is a class-level annotation that is used to declare which active bean definition profiles should be used when loading an ApplicationContext for test classes. 

它只能用在測試環境中,其他地方沒有用。

第一次用這個注解,在test環境中,src/test/resoutces寫下了這樣的application.yml

看起來非常靈活,

rpcconfig:
    dataconfig:
        uniformconfig:
            client-reconnect-try: 20
            #客戶端連接超時時間(單位:毫秒
            client-connect-timeout: 5000
            #token最大生命期(單位:毫秒)
            token-life: 1800000
            client-write-idle: 3000
            server-read-idle: 8000
        serviceconfig:
            #自定義配置的服務數量不確定
            - name: customer
              ip: 127.0.0.1
              port: 8001
            - name: account
              ip: 127.0.0.1
              port: 8002
            - name: dao
              ip: 127.0.0.1
              port: 8003
            - name: testsso
              ip: 127.0.0.1
              port: 8004
    ssoconfig:
        - leader-service: customer
          login-addr: /customer/login
          visitor-name: httpserver
          group-members:
           - account
           - testssos
---
spring:
    profiles:mytest
testdata:
    animals:
        - cat
        - dog
    map:
        cat: miao
        dog: wa
    bigmap:
        cat:
          name: mi
          color: white
        dog:
          name: wa
          color: yellow
    netdata:
        rpclient:
          threadname: rpcclient
          daemon: false
          bossnum: 0
          workernum: 4
        rpcserver:
          threadname: rpcserver
          daemon: false
          bossnum: 1
          workernum: 4
---
spring:
    profiles: cargraph
cardesign:
    paper:
        color: white
        count: 7
    pencil:
        length: 20
        brand: chenguang
---
spring:
    profiles: testmap
animal:
    author: kourui
    voice:
        cat: miao
        dog: wang
        tiger: ao
---

在src/main/java的configuration bean中隨意指定profile:

@Configuration
@ActiveProfiles("mytest")--------------------------------------------------//在application.yml任意profile好像都可以指定,寫起來很溜
@ConfigurationProperties(prefix="testdata")

然而,這種寫法只能在測試環境中應用。

@RunWith(SpringRunner.class)
@SpringBootTest

public class Testymldemo {
    Logger log = LoggerFactory.getLogger(Testymldemo.class);

    @Autowired
    private Uniformconfig ufc;
    
    @Autowired
    private Ssoconfig scf;
    
    @Autowired
    private TestConf tf;
    
    @Autowired
    private Serviceconfig serviceconfig;
    
    @Autowired
    private TestNetConf tncf;



    @Test
    public void testuniformconfig(){
        //this.resource
        //System.out.println(y0.getUniformconfig().getClientConnectTimeout());
        System.out.println(ufc.toString());
        System.out.println(scf.getSsoconfig());
        System.out.println(tf.getAnimals()+" "+tf.getMap()+" "+tf.getBigmap());
        System.out.println(serviceconfig.toString());
        System.out.println(tncf);
    }

如果將@ActiveProfiles用在正式運行的環境中,springboot直接忽略,然后達不到預期的“靈活”目的;

在正式運行環境中,怎么實現profile切換?

一種方法:

spring:
    profiles:
        active: dev     #在這里修改,,dev,,test,,....
registry:
    serverip: 127.0.0.1
    serverport: 9090
---
spring:
    profiles: test
registry:
    serverip: 127.0.0.1
    serverport: 9091
---
spring:
    profiles: dev
registry:
    serverip: 127.0.0.1
    serverport: 9092

其他方法:

https://blog.csdn.net/guinailu/article/details/86613347


免責聲明!

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



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