java - 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 不使用属性文件读取配置。你能帮我吗?
解决方案
在 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
推荐阅读
- c++ - 为什么我的 Qt 文件对话框的原生性取决于环境变量?
- android - set.connect 仅适用于第一个按钮
- python - TypeError:需要一个类似字节的对象,而不是“绘图”
- java - 设置为null后如何将onItemClickListener重置为true?
- angular - 在一个角度组件中一次访问所有属性绑定
- reactjs - 使用 React JS 输入地址并显示街景图像
- javascript - array.findIndex 不是函数
- javascript - 奇怪的是,window.document 没有返回理想情况下应该返回的正确对象。为什么?
- touchscreen - 我可以将此触摸屏与我的 ESP32 控制器一起使用吗?
- amp-html - amp-consent 和 IAB 同意字符串实施详细信息