我在包 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 );
}
}
现在我想使用 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不读取属性文件的配置。你能帮我吗?
最佳答案
在 Spring Boot 中,不需要添加 @PropertySource("classpath:/api.properties"),只需在 src/main/resources 中创建一个 application.properties Spring Boot 将获取所有属性,您可以在 SomeAPIConnector 中使用它们,
在 src/main/resources 中 应用程序属性 内容可以是:
some.data.token.header = XYZ
看看这个:http://www.springboottutorial.com/spring-boot-application-configuration
关于java - SpringBootApplication无法识别属性配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52008646/