Springboot Custom YML configuration file __springboot

Source: Internet
Author: User

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; }  
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.