SpringBootApplication 不识别属性配置 [英] SpringBootApplication does not recognize properties configuration
问题描述
我在 psn.stats.config
@Configuration
@PropertySource("classpath:/api.properties")
public class AppConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
我的 main
位于 psn.stats
包中,如下所示:
My main
is in package psn.stats
and looks like this:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class StatsServiceApplication {
public static void main( String[] args ) {
SpringApplication.run( StatsServiceApplication.class, args );
}
}
现在我想使用 psn.stats.connectors
包中的服务类中的属性值.这门课在这里:
Now I want to use properties values in on of the service class from package psn.stats.connectors
. This class is here:
@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 不读取带有属性文件的配置.你能帮我吗?
But when I run this app, all above fields are null. I don't know why SpringBoot does not read configuration with properties file. Can you help me with it?
推荐答案
spring boot 中不需要添加@PropertySource("classpath:/api.properties") 只需要在src/main/resources中创建一个application.propertiesspring boot 将获取所有属性,您可以在 SomeAPIConnector 中使用它们,
In spring boot it is not necessary to add @PropertySource("classpath:/api.properties") only create an application.properties in src/main/resources spring boot will get all the properties and you can use them in SomeAPIConnector,
在 src/main/resources应用程序属性内容可能是:
In src/main/resources application.properties the content could be:
some.data.token.header = XYZ
some.data.token.header = XYZ
看看这个:http://www.springboottutorial.com/spring-boot-应用程序配置
这篇关于SpringBootApplication 不识别属性配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!