How does springboot load a custom yml file through @ PropertySource
Editor to share with you how springboot loads custom yml files through @ PropertySource, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.
@ PropertySource loads a custom yml file
Using @ PropertySource loads .xml or .properties files by default, because the DefaultPropertySourceFactory implementation is used by default to process the file contents in the annotated source code, and spring uses ResourcePropertySource to build Properties from Resource to Spring.
Applications of the system, such as loading custom files, store the contents of the configuration file in memory, as follows:
To load a custom .yml file, you need a custom implementation ResourcePropertySource to handle the classes of the yml file
Public class YamlPropertySourceFactory implements PropertySourceFactory {@ Override public PropertySource createPropertySource (String name, EncodedResource resource) throws IOException {Properties propertiesFromYaml = loadYamlIntoProperties (resource); String sourceName = name! = null? Name: resource.getResource (). GetFilename (); return new PropertiesPropertySource (sourceName, propertiesFromYaml);} private Properties loadYamlIntoProperties (EncodedResource resource) throws FileNotFoundException {try {YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean (); factory.setResources (resource.getResource ()); factory.afterPropertiesSet (); return factory.getObject () } catch (IllegalStateException e) {/ / for ignoreResourceNotFound Throwable cause = e.getCause (); if (cause instanceof FileNotFoundException) throw (FileNotFoundException) e.getCause (); throw e;}} @ PropertySource annotation support for yml
@ PropertySource can only be loaded for properties files, but not for yml or yaml.
Pursue the source code.
Public class DefaultPropertySourceFactory implements PropertySourceFactory {public DefaultPropertySourceFactory () {} public PropertySource createPropertySource (String name, EncodedResource resource) throws IOException {return name! = null? New ResourcePropertySource (name, resource): new ResourcePropertySource (resource);}}
We just need to inherit the DefaultPropertySourceFactory class and modify it.
Public class YamlConfigFactory extends DefaultPropertySourceFactory {@ Override public PropertySource createPropertySource (String name, EncodedResource resource) throws IOException {String sourceName = name! = null? Name: resource.getResource (). GetFilename (); if (! resource.getResource (). Exists ()) {return new PropertiesPropertySource (sourceName, new Properties ());} else if (sourceName.endsWith (".yml") | | sourceName.endsWith (".yaml")) {Properties propertiesFromYaml = loadYml (resource); return new PropertiesPropertySource (sourceName, propertiesFromYaml);} else {return super.createPropertySource (name, resource) } private Properties loadYml (EncodedResource resource) throws IOException {YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean (); factory.setResources (resource.getResource ()); factory.afterPropertiesSet (); return factory.getObject ();} @ PropertySource (value = {"classpath:dog.yml"}, factory = YamlConfigFactory.class) @ Component@ConfigurationProperties (prefix = "dog") public class Dog {private String name; private String age The above is all the content of the article "how springboot loads custom yml files through @ PropertySource". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!