'Load spring properties from library module properties file

I have a spring application where I have created a library module for accessing AWS Cognito that are going to be used by multiple applications. The service needs a couple of properties in order to work which will be the same everywhere. So I would like to have the properties file in the library module and force the properties to be loaded from there whenever the library is imported.

I have tried creating a file called cognito-properties.yml in the library's resource folder and created a configuration file that is supposed to read from it.

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackageClasses = CognitoService.class)
@PropertySource("classpath:/cognito-properties.yml")
public class CognitoConfiguration {
    @Value("${cognito.accessKey}")
    private String accessKey;
    @Value("${cognito.secretKey}")
    private String secretKey;

    @Bean
    public AWSCognitoIdentityProvider awsCognitoIdentityProvider() {
        return AWSCognitoIdentityProviderClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(
                new BasicAWSCredentials(accessKey, secretKey)))
            .withRegion(Regions.EU_NORTH_1)
            .build();
    }
}

But I can't get the properties to load from the properties.yml file in the library. When I add the properties to the applications application.yml file it works fine.



Solution 1:[1]

Turns out that you can't use @PropertySource with a yml file. It needs to be a .properties file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1