io.elsci.springutils.EnvironmentPropertySource Maven / Gradle / Ivy
package io.elsci.springutils;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
/**
* Spring doesn't have this - instead it defines an anonymous class inside {@link PropertySourcesPlaceholderConfigurer}
* which doesn't allow us to create a list of {@link PropertySource} manually (facepalm). So if we want to explicitly
* tell Spring's {@link PropertySourcesPlaceholderConfigurer} which property sources it can use (e.g. because the
* default list isn't enough), AND we want to keep reading env variables, we can use this implementation.
*/
public class EnvironmentPropertySource extends PropertySource implements EnvironmentAware {
// Either a StandardEnvironment, or StandardServletEnvironment, or something else depending on whether we
// start the tests or the Servlet Container. It already has PropertySources inside that can resolve environment
// variables, so we don't have to do this ourselves.
private Environment environment;
public EnvironmentPropertySource() {
this(PropertySourcesPlaceholderConfigurer.ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME);
}
public EnvironmentPropertySource(String propSourceName) {
super(propSourceName);
}
@SuppressWarnings("NullableProblems")
@Override
public Object getProperty(String name) {
return environment.getProperty(name);
}
@SuppressWarnings("NullableProblems")
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy