SpringBootApplication无法识别属性配置

时间:2018-08-24 16:41:25

标签: 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不读取带有属性文件的配置。你能帮我吗?

1 个答案:

答案 0 :(得分:0)

在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