java - SpringBootApplication无法识别属性配置

标签 java spring-boot properties

我在包 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/

相关文章:

java - 使用 webdriver 在 selenium 中处理灯箱/光窗

java - 从后台运行启动服务程序的命令(通过命令行)

java - Ant:如何知道十个复制任务中的至少一个复制任务是否真正复制了文件

javascript - Vue.js 2,从指令更改数据

java - 使用正则表达式验证输入字符串 "RX-EZ12345678912345B"

java - 我将如何使用 while 循环来不断请求用户输入

java - 如何使用 Shiro 的 Salted AuthenticationInfo?

java - 从配置中动态读取@JsonProperty

spring-boot - Spring Boot 2.0 禁用默认安全性

java - Eclipse 中特殊属性的编码