简体   繁体   中英

SpringBootApplication does not recognize properties configuration

I have a simple properties configuration in package psn.stats.config

@Configuration
@PropertySource("classpath:/api.properties")
public class AppConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

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 );
    }
}

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;
}

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?

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,

In src/main/resources application.properties the content could be:

some.data.token.header = XYZ

check out this: http://www.springboottutorial.com/spring-boot-application-configuration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM