In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.