Overview
We know that in spring boot, you can introduce your own configuration file through XML or @importresource, but there is a limit, which must be local, and the format can only be properties (or YAML). So, if we have a remote configuration, how to bring him in. If you are interested, you can read another blog about how Spring Cloud is introducing the remote configuration Spring Cloud Configuration service. Of course not see nor affect the reading behind.
How to Do
In fact, it only takes 3 steps to customize the configuration source
The first step is to write the Propertysource
Write a class to inherit Enumerablepropertysource, and then implement its abstract method, the abstract method to see the role of name, simple, here is a map to save the configuration, for example:
Public classMypropertysourceextendsEnumerablepropertysource<map<string,string>> { PublicMypropertysource (String name, Map source) {Super(name, source); } //get all the configuration names@Override Publicstring[] Getpropertynames () {returnSource.keyset (). ToArray (Newstring[source.size ()]); } //returns the corresponding property based on the configuration@Override PublicObject GetProperty (String name) {returnsource.get (name); }}
The second step is to write the Propertysourcelocator
Propertysourcelocator is actually used to locate the propertysource in front of us, the only way to rewrite it is to return a Propertysource object, for example,
public class Mypropertysourcelocator implements Propertysourcelocator {@Override public Propertysource<?> Locate (environment environment) { // for simplicity, create a map directly here where you can write where to get configuration information. map<string,string> properties = new hashmap<> (); Properties.put ( "MyName", "Lizo" = new mypropertysource (" Mypropertysource ",properties); return Mypropertysource; }}
Step three, let propertysourcelocator take effect.
Create a new configuration class, such as
@Configuration Public class myconfigbootstrapconfiguration { @Bean public mypropertysourcelocator Mypropertysourcelocator () { returnnew mypropertysourcelocator (); }}
Finally Create/update meta-info/spring.factories (if you have done custom spring boot development know this file)
org.springframework.cloud.bootstrap.bootstrapconfiguration=com.lizo.MyConfigBootstrapConfiguration
Simply put to spring boot, this is a boot configuration class (a high-priority configuration Class).
Write a test test
@SpringBootApplication Public classTest2 { Public Static voidMain (string[] args)throwsSQLException {configurableapplicationcontext run= Springapplication.run (Test2.class, args); Ser Bean= Run.getbean (Ser.class); System.out.println (Bean.getmyname ()); } @Component Public Static classser{@Value ("${myname}") PrivateString MyName; PublicString Getmyname () {returnMyName; } Public voidsetmyname (String myName) { This. MyName =MyName; } }}
Correct output
Test Two
We introduce this variable in the application configuration file, for example in Application.properties
My.name=${myname}
Similarly, the results can be effective
MyName is the configuration attribute that was written in propertysourcelocator above. Run the program and you can see that the output is actually correct.
Summary
The above is just a feed, so no matter where the data source, can be written in this way, the configuration to spring management. This is no longer afraid to appear in the local configuration file sensitive information, no longer afraid to modify the configuration file needs to log on every machine modified.
How to customize the configuration source to spring boot management