How to load application.properties in Spring Boot multi-module project

In a pom.xml file, there are multiple modules, and the project structure is as follows:

Parent-Project
|--MainApplication
|--Module1
|--ModuleN

There is a main method and annotation SpringBootApplication in the project MainApplication. In this project application.properties is automatically loaded, so I can access the relevant key through @Value.

@Value("${myapp.api-key}")
private String apiKey;

在 Module1中我也想使用一个properties文件(称为module1.properties),
Within my Module1 I want to use a properties file as well (called module1.properties), where the modules configuration is stored. This File will only be accessed and used in the module. But I cannot get it loaded. I tried it with @Configuration and @PropertySource but no luck.

@Configuration
@PropertySource(value = "classpath:module1.properties")
public class ConfigClass {

How can I load a properties file with Spring-Boot and access the values easily? Could not find a valid solution.

My Configuration

@Configuration
@PropertySource(value = "classpath:tmdb.properties")
public class TMDbConfig {

    @Value("${moviedb.tmdb.api-key}")
    private String apiKey;

    public String getApiKey() {
        return apiKey;
    }

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

Calling the Config

@Component
public class TMDbWarper {

@Autowired
private TMDbConfig tmdbConfig;

private TmdbApi tmdbApi;

public TMDbWarper(){
    tmdbApi = new TmdbApi(tmdbConfig.getApiKey());
}

I’m getting an NullPointerException in the constructor when I autowire the warper.

Reference documentation
https://stackoverflow.com/questions/46784051/spring-boot-multi-module-project-load-property-file

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324677122&siteId=291194637