com.ksoot.common.spring.boot.DefaultPropertiesEnvironmentPostProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-boot-commons Show documentation
Show all versions of spring-boot-commons Show documentation
Commons Spring boot components and Utilities
The newest version!
package com.ksoot.common.spring.boot;
import com.ksoot.common.ConfigException;
import com.ksoot.common.spring.util.ExternalFileLoaderUtil;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;
/**
* @author Rajveer Singh
*/
@Log4j2
@Order
public class DefaultPropertiesEnvironmentPostProcessor implements EnvironmentPostProcessor {
private static final String DEFAULT_PROPERTIES = "config/defaults.yml";
private static final String DEFAULT_PROPERTIES_SOURCE_NAME = "defaultProperties";
@Override
public void postProcessEnvironment(
ConfigurableEnvironment environment, SpringApplication application) {
log.debug("Adding default properties from : " + DEFAULT_PROPERTIES);
Properties defaultProperties = new Properties();
try {
Resource extFile = new ClassPathResource(DEFAULT_PROPERTIES);
if (extFile.exists()) {
defaultProperties.putAll(ExternalFileLoaderUtil.loadProperties(extFile));
log.info("Default property file: " + DEFAULT_PROPERTIES + " added in property sources");
} else {
throw new ConfigException("File not found: " + DEFAULT_PROPERTIES);
}
} catch (IOException e) {
throw new ConfigException(
"Exception while reading default properties file: " + DEFAULT_PROPERTIES, e);
}
if (!defaultProperties.isEmpty()) {
MutablePropertySources propertySources = environment.getPropertySources();
PropertySource> existingDefaultProperties =
propertySources.remove(DEFAULT_PROPERTIES_SOURCE_NAME);
if (existingDefaultProperties != null) {
Object src = existingDefaultProperties.getSource();
if (ClassUtils.isAssignableValue(Map.class, src)) {
defaultProperties.putAll((Map, ?>) src);
} else {
log.error(
"Unknown default property source type: " + src.getClass() + ", handle accordingly");
}
} else {
log.info("No default properties found before adding external default properties");
}
// application.setDefaultProperties(defaultProperties);
propertySources.addLast(
new PropertiesPropertySource(DEFAULT_PROPERTIES_SOURCE_NAME, defaultProperties));
} else {
log.debug("No external default properties defined");
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy