refer to:/2018-07-04-PropertySource-with-yaml-files/
background
When customizing modules, some core yml configurations of the current module are often written. If you want to load into Spring containers, the usual practice is to name the yml of the custom module asapplication-module name.yml
For example, the db module is named, then
Introduce this db to load it into the submodule configuration (provided that the resources resource directory can be compiled in when maven is built).
There are many restrictions mentioned above. Submodules are black box operations for parent modules. We cannot know how many submodules are to be included, and the prefix must start with application.
Today we will look at another more flexible way. The yml with the custom name is loaded directly in the submodule. Friends continue to read.PropertySourceFactory
How to achieve it.
Summary 3 steps
Define aYamlPropertySourceFactory
,accomplishPropertySourceFactory
, rewritecreatePropertySource
method
Load the resource stream file to the interface tospring
ProvidedYmlPropertiesFactorBean
In the factoryBean
Produce oneProperties
Object
Will be generatedProperties
Object incomingPropertiesPropertySource
One out ofPropertySource
Object
import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * @description: * @author: * @date: Created in 2020/9/24 12:05 */ @AllArgsConstructor public class YamlPropertySourceFactory implements PropertySourceFactory { //This method has two parameters, name and resource object, this is the first step public PropertySource< ? > createPropertySource(String name, EncodedResource resource) throws IOException { //This is the second step, producing Properties object based on the flow object Properties propertiesFromYaml = loadYamlIntoProperties(resource); String sourceName = name != null ? name : ().getFilename(); assert sourceName != null; //This is the third part. According to the Properties object, the PropertySource object is produced and put into Spring return new PropertiesPropertySource(sourceName, propertiesFromYaml); } private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException { try { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); (()); (); return (); } catch (IllegalStateException e) { Throwable cause = (); if (cause instanceof FileNotFoundException) { throw (FileNotFoundException) (); } throw e; } } }
We've finished creating it. My friends have many question marks, how to use it?
Next, let’s look at Kang Kang:
In any one@Configuration
In the class, you can write one by yourself, anyway, it can beSpring
Scanned
Add the following code, which is actually just one line:
@Configuration //The YamlPropertySourceFactory in this place is the code class written above@PropertySource(factory = , value = "classpath:") public class XXXXConfiguration { }
at last
OK, it's over here, go and use it to see the effect~
The above is personal experience. I hope you can give you a reference and I hope you can support me more.