玄学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

原文地址:https://www.cnblogs.com/CreatorKou/p/11350534.html