Springboot after version 1.5, the location parameter was removed @ConfigurationProperties, so the YML configuration file could not be exported directly.
The properties configuration file can be imported directly through @propertysource or @importresource, so that two kinds of import custom yml files are sorted
1. Use Yamlpropertiesfactorybean to load and set to Propertysourcesplaceholderconfigurer
Code:
@Bean public
propertysourcesplaceholderconfigurer Yaml () {
propertysourcesplaceholderconfigurer Configurer = new Propertysourcesplaceholderconfigurer ();
Yamlpropertiesfactorybean Yaml = new Yamlpropertiesfactorybean ();
Yaml.setresources (New Classpathresource ("Myconfig.yml"));
Configurer.setproperties (Yaml.getobject ());
return configurer;
}
2, the use of @propertysource, custom factory
Code:
Import java.io.IOException;
Import Org.springframework.boot.env.PropertySourcesLoader;
Import Org.springframework.core.env.PropertySource;
Import Org.springframework.core.io.Resource;
Import Org.springframework.core.io.support.EncodedResource;
Import Org.springframework.core.io.support.PropertySourceFactory; public class Yamlpropertysourcefactory implements Propertysourcefactory {@Override public propertysource<?> Crea Tepropertysource (String name, Encodedresource Resource) throws IOException {return name!= null? New PROPERTYSOURCESL Oader (). Load (Resource.getresource (), name, NULL): New Propertysourcesloader (). Load (resource.getresour
CE (), Getnameforresource (Resource.getresource ()), NULL);
private static String Getnameforresource (Resource Resource) {String name = Resource.getdescription (); if (!org.springframework.util.stringutils.hastext (name)) {name = Resource.getclass (). Getsimplename () + "@" + System.identiTyhashcode (Resource);
} return name; }
}