RefreshScope Not working when overriding PropertySourcesPlaceholderConfigurer

32 views Asked by At

As per my requirement, i want to update configuration properties before they are used in creating beans. for that, i am extending PropertySourcesPlaceholderConfigurer and overriding loadProperties method. below is my code and this works perfectly. however when i update the properties using /env and /refresh endpoint, properties are not updated. and if i remove below code, properties are updated perfectly. what am i doing wrong ?

@Component
@Slf4j
public class PasswordUpdatePropertyConfigurer extends PropertySourcesPlaceholderConfigurer {
    private ConfigurableEnvironment environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = (ConfigurableEnvironment) environment;
    }

    @Override
    protected void loadProperties(Properties props) {
        
        for (PropertySource<?> propertySource : environment.getPropertySources()) {
            if (propertySource instanceof EnumerablePropertySource) {
                String[] propertyNames = ((EnumerablePropertySource) propertySource).getPropertyNames();
                for (String propertyName : propertyNames) {
                    String configValue = String.valueOf(propertySource.getProperty(propertyName));
                    String secretValue = configValue;
                    if (StringUtils.isNotEmpty(configValue) && configValue.startsWith("password")) {
                            secretValue = configValue.replace("password","niti");  
                    }
                    if (!props.containsKey(propertyName)) {
                        props.setProperty(propertyName, secretValue);
                    }
                }
            }
        }
    }
}

0

There are 0 answers