Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Springboot project custom read multi-environment yml configuration method

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "springboot project custom read multi-environment yml configuration method", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "springboot project custom read multi-environment yml configuration method" bar!

There are some payment-related configuration files that you don't want to write into application.

Forget the principle, there are a lot of them on the Internet.

1: go directly to the code:

Import lombok.extern.slf4j.Slf4j;import org.springframework.boot.SpringApplication;import org.springframework.boot.env.EnvironmentPostProcessor;import org.springframework.boot.env.PropertySourceLoader;import org.springframework.boot.env.YamlPropertySourceLoader;import org.springframework.core.env.ConfigurableEnvironment;import org.springframework.core.env.PropertySource;import org.springframework.core.io.Resource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.core.io.support.ResourcePatternResolver;import org.springframework.util.CollectionUtils;import org.springframework.util.ResourceUtils Import java.io.IOException;import java.util.List;/** * @ version V1.0 * @ description: environment processor for reading custom payment-*.yml (read-only yml files, other files are ignored) * @ author: SunF * @ create: 2019-08-21 09:59 * * / @ Slf4jpublic class PaymentEnvironmentPostProcessor implements EnvironmentPostProcessor {private final ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver (); private final PropertySourceLoader propertySourceLoader; public PaymentEnvironmentPostProcessor () {super () PropertySourceLoader = new YamlPropertySourceLoader ();} @ Override public void postProcessEnvironment (ConfigurableEnvironment environment, SpringApplication application) {String [] activeProfiles = environment.getActiveProfiles (); for (String activeProfile: activeProfiles) {String location = ResourceUtils.CLASSPATH_URL_PREFIX + "paymentConfig/payment-" + activeProfile+ ".yml"; try {Resource [] resourceArray = this.patternResolver.getResources (location) For (Resource resource: resourceArray) {List

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report