if you need to spring load many properties files (diff names) with same key,
bellow impl in spring boot cloud config client (using springCloudConfigServer to load files) and add prefix for props is:
@RefreshScope
@Configuration
public class CustomProperties implements ApplicationListener {
private static final Logger LOGGER = LoggerFactory.getLogger(CustomProperties.class);
private static final String BOOTSTRAP_PROPERTIES = "bootstrapProperties";
@Autowired
private ConfigurableEnvironment configurableEnvironment;
@PostConstruct
public void loadPropertySources() {
try {
final Collection originTrackedMapPropertySources = new ArrayList();
for (PropertySource propertySource : configurableEnvironment.getPropertySources()) {
if (BOOTSTRAP_PROPERTIES.equalsIgnoreCase(propertySource.getName()) && propertySource instanceof OriginTrackedCompositePropertySource) {
OriginTrackedCompositePropertySource bootstrapProperties = (OriginTrackedCompositePropertySource) propertySource;
for (PropertySource bootstrapPropertiesSource : bootstrapProperties.getPropertySources()) {
OriginTrackedCompositePropertySource originTrackedCompositePropertySource = (OriginTrackedCompositePropertySource) bootstrapPropertiesSource;
for (PropertySource propertySourceOriginTrackedCompositePropertySource : originTrackedCompositePropertySource.getPropertySources()) {
OriginTrackedMapPropertySource originTrackedMapPropertySource = (OriginTrackedMapPropertySource) propertySourceOriginTrackedCompositePropertySource;
final String originTrackedMapPropertySourceName = originTrackedMapPropertySource.getName();
final Map newPropertySource = new TreeMap();
String newPropertySourceName = originTrackedMapPropertySourceName.substring(originTrackedMapPropertySourceName.lastIndexOf('/') + 1, originTrackedMapPropertySourceName.lastIndexOf('.'));
newPropertySourceName = newPropertySourceName.toLowerCase().replace('-', '.');
for (String propertyName : originTrackedMapPropertySource.getPropertyNames()) {
final Object propertyValue = originTrackedMapPropertySource.getProperty(propertyName);
newPropertySource.put(newPropertySourceName + "." + propertyName, propertyValue);
}
originTrackedMapPropertySources.add(new OriginTrackedMapPropertySource(newPropertySourceName, newPropertySource));
}
}
}
}
//
for (OriginTrackedMapPropertySource originTrackedMapPropertySource : originTrackedMapPropertySources) {
LOGGER.info("PropertySourceName Added: {}", originTrackedMapPropertySource.getName());
configurableEnvironment.getPropertySources().addLast(originTrackedMapPropertySource);
}
LOGGER.info("Finish properties add");
} catch (Exception e) {
LOGGER.error("Ex on configure property sources", e);
}
}
@Override
public void onApplicationEvent(EnvironmentChangeEvent environmentChangeEvent) {
loadPropertySources();
}
}
Works fine in /actuator/refresh
and to use @Value('${you.propertiesname.your.key}')
or @ConfigurationProperties(prefix = "you.propertiesname")
And works with another uses for getProperty env