properties、EnvironmentPostProcessor springboot多配置文件
动态添加properties
@Configuration public class EnvironmentConfig { @Autowired private ConfigurableEnvironment environment; @PostConstruct public void addEnvironmentProperties() { Map<String, Object> map = new HashMap<>(); map.put("my.custom.property", "myValuexx"); MapPropertySource propertySource = new MapPropertySource("property", map); environment.getPropertySources().addFirst(propertySource); } } //只能从environment中获取 ApplicationContextHolder自定义类,获取上下文 Environment environment = ApplicationContextHolder.getContext().getEnvironment(); String es = environment.getProperty("my.custom.property");
总结:yaml与properties互通,ymal覆盖properties
1、yaml中的配置可以在TestProperties实例中获取,会覆盖properties配置 @ConfigurationProperties(prefix = "my.custom") @PropertySource(value = "customProperties.properties") public class TestProperties { 2、可以通过$方式获取properties中的值,yaml值会覆盖properties配置 @Value("${my.custom.machine:s}") private String testXmh;
1,src\main\resources\META-INF\spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\other.impl.MyInterfaceImpl
2,
@Configuration
@Import(MyInterfaceImpl.class)
把测试类放到外层,不能被springboot扫描到other.IMyInterface
二者同等效果,把非本项目的类,实例化到上下文中
多环境单配置文件 @Value("${testx}") private String testx; application.properties spring.profiles.active=@spring.profiles.active@ application-dev.properties testx=testxmhdev application-test.properties testx=testxmhtest <profiles> <profile> <id>dev</id> <properties> <env>dev</env> <spring.profiles.active>dev</spring.profiles.active> </properties> </profile> <profile> <id>test</id> <properties> <env>test</env> <spring.profiles.active>test</spring.profiles.active> </properties> </profile> </profiles> 多环境多配置文件 src/main/resources/META-INF/spring.factories org.springframework.boot.env.EnvironmentPostProcessor=\cn.com.config.Configs src/main/resources/config/dev/configTest1.properties src/main/resources/config/dev/configTest2.properties src/main/resources/config/test/configTest1.properties src/main/resources/config/test/configTest2.properties
//@Configuration public class Configs implements EnvironmentPostProcessor, Ordered { // private Logger logger = LoggerFactory.getLogger(Configs.class); @Override @SuppressWarnings({"unchecked", "rawtypes"}) public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { System.out.println("postProcessEnvironment******profiles:"+environment.getActiveProfiles()); //忽略不可解析的 `${xxx}` environment.setIgnoreUnresolvableNestedPlaceholders(true); MutablePropertySources propertySources = environment.getPropertySources(); Properties props = getConfig(environment); propertySources.addLast(new PropertiesPropertySource("xmh", props));//thirdEnv for (PropertySource<?> propertySource : propertySources) { if (propertySource.getSource() instanceof Map) { Map map = (Map)propertySource.getSource(); for (Object key : map.keySet()) { String keyStr = key.toString(); Object value = environment.getProperty(keyStr); // if ("druid.password,druid.writer.password,druid.reader.password".contains(keyStr)) { // String dkey = (String)map.get("druid.key"); // dkey = DataUtil.isEmpty(dkey) ? Constants.DB_KEY : dkey; // value = SecurityUtil.decryptDes(value.toString(), dkey.getBytes()); // map.put(key, value); // } // PropertiesUtil.getProperties().put(keyStr, value.toString()); } } } } @Override public int getOrder() { return ConfigFileApplicationListener.DEFAULT_ORDER + 1; } // 加载配置文件 private Properties getConfig(ConfigurableEnvironment environment) { String[] profiles = environment.getActiveProfiles(); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); List<Resource> resouceList = new ArrayList<>(); loadResource(resolver,resouceList,profiles,"classpath*:config"); try { PropertiesFactoryBean config = new PropertiesFactoryBean(); config.setLocations(resouceList.toArray(new Resource[]{})); config.afterPropertiesSet(); return config.getObject(); } catch (IOException e) { throw new RuntimeException(e); } } private void loadResource(PathMatchingResourcePatternResolver resolver, List<Resource> resouceList, String[] profiles, String configPath) { addResources(resolver, resouceList, configPath+"/*.properties"); for (String p : profiles) { if (p != null || "".equals(p)) { p = p + "/"; } addResources(resolver, resouceList, configPath+"/" + p + "*.properties"); } } private void addResources(PathMatchingResourcePatternResolver resolver, List<Resource> resouceList, String path) { try { Resource[] resources = resolver.getResources(path); for (Resource resource : resources) { resouceList.add(resource); } } catch (Exception e) { e.printStackTrace(); } } }