首页 > 解决方案 > SpringBootApplication 无法识别属性配置

问题描述

我在包中有一个简单的属性配置psn.stats.config

@Configuration
@PropertySource("classpath:/api.properties")
public class AppConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

main的在包装中psn.stats,看起来像这样:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class StatsServiceApplication {

    public static void main( String[] args ) {
        SpringApplication.run( StatsServiceApplication.class, args );
    }
}

现在我想在 package 的服务类中使用属​​性值psn.stats.connectors。这堂课在这里:

@Component
public class SomeAPIConnector {

    @Value( "${some.data.token.header}" )
    private String tokenHeader;
    @Value( "${some.data.token.value}" )
    private String token;
    @Value( "${some.data.api.address}" )
    private String apiAddress;
}

但是当我运行这个应用程序时,以上所有字段都是空的。我不知道为什么 SpringBoot 不使用属性文件读取配置。你能帮我吗?

标签: javaspring-bootproperties

解决方案


在 spring boot 中不需要添加 @PropertySource("classpath:/api.properties") 只需在 src/main/resources 中创建一个 application.properties spring boot 将获取所有属性,您可以在 SomeAPIConnector 中使用它们,

在 src/main/resources application.properties 中的内容可以是:

some.data.token.header = XYZ

看看这个:http ://www.springboottutorial.com/spring-boot-application-configuration


推荐阅读