java / expert
Snippet
Extending PropertySourceFactory for Encrypted YAML
By implementing PropertySourceFactory, you can intercept the configuration loading process. This allows for advanced patterns like decrypting secrets stored in YAML files at the moment they are injected into the Spring Environment.
snippet.java
1
2
3
4
5
6
7
8
9
10
11
public class EncryptedYamlFactory implements PropertySourceFactory {@Overridepublic PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();factory.setResources(resource.getResource());Properties props = factory.getObject();// Custom decryption logic for values starting with '{cipher}'decryptValues(props);return new PropertiesPropertySource(name != null ? name : resource.getResource().getFilename(), props);}}
spring
Breakdown
1
implements PropertySourceFactory
The interface used to customize how @PropertySource handles file formats or pre-processing.
2
decryptValues(props);
A placeholder for industrial-grade decryption logic (e.g., using AES or a Key Management Service) before properties hit the context.