package com.vincent.rsf.httpaudit.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.env.YamlPropertySourceLoader; import org.springframework.core.Ordered; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertySource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import java.io.IOException; import java.util.List; /** * jar 内默认配置,最低优先级;引用工程覆盖。 */ public class HttpAuditJarDefaultsEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { private static final Logger log = LoggerFactory.getLogger(HttpAuditJarDefaultsEnvironmentPostProcessor.class); private static final String RESOURCE = "META-INF/rsf-http-audit/defaults.yml"; private static final String NAME = "httpAuditJarDefaults"; @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { if (environment.getPropertySources().contains(NAME)) { return; } Resource resource = new ClassPathResource(RESOURCE); if (!resource.exists()) { return; } try { YamlPropertySourceLoader loader = new YamlPropertySourceLoader(); List> loaded = loader.load(NAME, resource); for (PropertySource ps : loaded) { environment.getPropertySources().addLast(ps); } } catch (IOException e) { log.warn("http_audit_warn code=jar_defaults_load_failed", e); } } @Override public int getOrder() { return Ordered.LOWEST_PRECEDENCE; } }