I have this code that scans Spring context:
public void scan() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(SomeConfig.class);
context.refresh();
}
I need properties to be read from application.yml file, so in SomeConfig class, I have this:
@Configuration
@PropertySource(value = "classpath:application.yml", factory = YamlPropertyLoaderFactory.class)
public class SomeConfig {
//some beans
}
(I have copied YamlPropertyLoaderFactory class from here)
application.yml is a typical Spring Boot file with some properties by profile, and a default profile:
spring:
profiles:
active: p1
---
spring:
profiles: p1
file: file1.txt
---
spring:
profiles: p2
file: file2.txt
In some bean, I'm reading file property using @Value.
When I run my application, I'm passing -Dspring.profiles.active=p1 variable, but I'm getting an error:
Could not resolve placeholder 'file' in value "${file}"
(It should work even if I don't pass any profile since application.yml has default profile set to p1)
If I remove all profiles config from application.yml, it works fine:
file: file1.txt
So, it means that context scan is not reading the profile variable.
Also, if I set active profile "programatically", it doesn't resolve the properties either:
context.getEnvironment().setActiveProfiles("p1");