SpringBoot扩展点之EnvironmentPostProcessor

 

在使用spring boot做开发时,有时我们需要自定义环境变量或者编写第三方扩展点,可以使用EnvironmentPostProcessor,注意如果你只是基本的使用环境,就不需要看此文了 

 

1.   属性文件myapp.properties,可以自定义文件名,也可以有多个同名文件

config/myapp.properties文件

app.url=https://github.com/dongguangming/
app.name=dgm
app.desc=dongguangming github
app.customComponentScanPackages=com.spring.mapper,com.spring.mapper
mysqluser=rootroot
mysqlpwd=cstorfscstorfs
app.who=who are you

myapp.properties.文件

app.url=https://github.com/dongguangming//
app.name=dgmdgm
app.desc=dongguangming github
app.customComponentScanPackages=com.spring.mapper,com.spring.mapper

 

2.   新建java实现类

/**
 * @author dgm
 * @describe "环境变量扩展点"
 * @date 2020年10月12日
 */
@Order(Ordered.LOWEST_PRECEDENCE)
public class MyJsonEnvironmentPostProcessor implements EnvironmentPostProcessor {

	private static final Logger logger = LoggerFactory
			.getLogger(MyJsonEnvironmentPostProcessor.class);

	private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";
	private static final String DEFAULT_NAMES = "myapp";
	private static final String DEFAULT_FILE_EXTENSION = ".properties";

	private static final String PREFIX = "com.spring.environmentpostprocessor.";
	private static final String CALCUATION_MODE = "calculation_mode";
	private static final String GROSS_CALCULATION_TAX_RATE = "gross_calculation_tax_rate";
	private static final String CALCUATION_MODE_DEFAULT_VALUE = "NET";
	private static final double GROSS_CALCULATION_TAX_RATE_DEFAULT_VALUE = 0;

	List<String> names = Arrays.asList(CALCUATION_MODE,
			GROSS_CALCULATION_TAX_RATE);

	private static Map<String, Object> defaults = new LinkedHashMap<>();

	static {
		defaults.put(CALCUATION_MODE, CALCUATION_MODE_DEFAULT_VALUE);
		defaults.put(GROSS_CALCULATION_TAX_RATE,
				GROSS_CALCULATION_TAX_RATE_DEFAULT_VALUE);
	}

	@Override
	public void postProcessEnvironment(ConfigurableEnvironment environment,
			SpringApplication application) {

		/*
		 * PropertySource<?> system = environment.getPropertySources()
		 * .get(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
		 * 
		 * Map<String, Object> prefixed = new LinkedHashMap<>();
		 * 
		 * if (!hasOurPriceProperties(system)) { // Baeldung-internal code so
		 * this doesn't break other examples logger.error(
		 * "System environment variables [calculation_mode,gross_calculation_tax_rate] not detected, fallback to default value [calcuation_mode={},gross_calcuation_tax_rate={}]"
		 * , CALCUATION_MODE_DEFAULT_VALUE,
		 * GROSS_CALCULATION_TAX_RATE_DEFAULT_VALUE); prefixed = names.stream()
		 * .collect(Collectors.toMap(this::rename, this::getDefaultValue));
		 * 
		 * environment.getPropertySources()
		 * .addAfter(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, new
		 * MapPropertySource("prefixer", prefixed));
		 * 
		 * return; }
		 * 
		 * prefixed = names.stream() .collect(Collectors.toMap(this::rename,
		 * system::getProperty)); environment.getPropertySources()
		 * .addAfter(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, new
		 * MapPropertySource("prefixer", prefixed));
		 */
		List<String> list = Arrays
				.asList(StringUtils.trimArrayElements(StringUtils
						.commaDelimitedListToStringArray(DEFAULT_SEARCH_LOCATIONS)));

		Collections.reverse(list);
		Set<String> reversedLocationSet = new LinkedHashSet(list);
		System.err.println(reversedLocationSet);

		ResourceLoader defaultResourceLoader = new DefaultResourceLoader();
		// YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new
		// YamlPropertiesFactoryBean();
		PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();

		List<Properties> loadedProperties = new ArrayList<>(16);
		// List<Resource> propertiesPathList = new ArrayList<>(16);

		reversedLocationSet.forEach(location -> {
			Resource resource = defaultResourceLoader.getResource(location
					+ DEFAULT_NAMES + DEFAULT_FILE_EXTENSION);
			System.err.println(location + DEFAULT_NAMES
					+ DEFAULT_FILE_EXTENSION);
			if (resource == null || !resource.exists()) {
				return;
			}
			System.err.println("################33");

			Properties p = new Properties();
			try {
				InputStream inputStream = resource.getInputStream();
				p.load(inputStream);
			} catch (Exception e) {
				e.printStackTrace();
			}

			loadedProperties.add(p);
		});

		System.err.println(loadedProperties);

		Properties filteredProperties = new Properties();
		Set<Objec
  • 8
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值