SoFunction
Updated on 2025-03-03

How to start SpringBoot to automatically load custom module yml file (PropertySourceFactory)

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.ymlFor example, the db module is named, thenIntroduce 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.PropertySourceFactoryHow to achieve it.

Summary 3 steps

Define aYamlPropertySourceFactory,accomplishPropertySourceFactory, rewritecreatePropertySourcemethod

Load the resource stream file to the interface tospringProvidedYmlPropertiesFactorBeanIn the factoryBeanProduce onePropertiesObject

Will be generatedPropertiesObject incomingPropertiesPropertySourceOne out ofPropertySourceObject

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@ConfigurationIn the class, you can write one by yourself, anyway, it can beSpringScanned

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.