0
点赞
收藏
分享

微信扫一扫

spring 配置类中返回的 bean怎么通过yaml初始化

Gaaidou 2024-08-06 阅读 39

本质无法找到数据源

采取了两个解决措施。

第一个:

因为项目的配置文件根据环境不同采取不同的配置,所以在测试文件中使用@ActiveProfiles指定环境,并且使用@ComponentScan注解导入bean

@SpringBootTest
@RunWith(SpringRunner.class)
@ComponentScan("com.xxxx")
@ActiveProfiles("dev")

第二个:

项目读取的是nacos配置中心里的文件,但是配置文件中使用了${}占位符读取别的文件里的信息,测试时可能无法识别占位符的信息导致无法读取配置中心里的文件,所以给占位符一个默认值

  cloud:
    nacos:
      discovery:
        server-addr: ${nacos.server:xxxx:8848}
        namespace: ${nacos.namespace:public}
        username: nacos
        password: nacos
      config:
        server-addr: ${nacos.server:xxxx:8848}
        namespace: ${nacos.namespace:public}
        username: nacos
        password: nacos
        file-extension: yaml
        prefix: empoworx-webreport
        shared-dataids: common-config.yaml
        refreshable-dataids: common-config.yaml
举报

相关推荐

0 条评论