How to use propertysource annotation in spring boot
preface
This article will focus on the use of the @ propertysource annotation in spring and how to load the specified configuration file through the propertysource annotation. And the combination of propertysource annotation and @ configurationproperties annotation. There's not much to say below. Let's learn together with Xiaobian.
1.1. The propertysource annotation loads the specified property file
The spring framework provides the propertysource annotation to load the specified property file. Next, let's see how to use the annotation. First, we define a configuration class and add the propertysource annotation in the class, as shown below:
The purpose of the above code is to load jdbc-bainuo-dev.properties in the config file in the classpath path. Encoding is used to specify the encoding used to read the attribute file. We usually use UTF-8; Ignoreresourcenotfound means whether an error is reported when the specified configuration file does not exist. The default is false; For example, the loading properties file specified above is jdbc-bainuo-dev.properties. If the file does not exist, the program will not report an error when ignoreresourcenotfound is true. If ignoreresourcenotfound is false, the program will directly report an error. In actual project development, it is best to set ignoreresourcenotfound to false. The default value of this parameter is false.
The value value is used to set the property file to be loaded, and multiple files can be loaded at one time. The value of name we set is jdbc-bainuo-dev.properties. This value must be unique in the springboot environment. If it is not set, the value is: "class path resource [config / jdbc-bainuo-dev.properties]".
Many people may wonder, Why "class path resource [config / JDBC Bainuo dev.properties]"? This involves the encapsulation class resource for resource files in spring. The value value we configured above is "classpath: config / JDBC Bainuo dev.properties" Therefore, the spring discovery starts with classpath, so the final use is classpathresource, a subclass of resource. If it starts with file, the final class used is filesystemresource.
After understanding the resource class described above. Let's make it clear again that if @PropertySource is not set name value, the generation rule of name value is to find the final encapsulated Resource subclass according to value value, then call the getDescription method in the specific Resource subclass instance object, and the return value of getDescription method is the final name value.
For example, the getdescription method in the classpathresource class is implemented as follows:
For the time being, you can have an impression of the above name processing logic. The source code will be tracked and explained in detail later.
1.2. The propertysource annotation loads the specified property file test
Above, we set the propertysource annotation to load the "classpath: config / jdbc-bainuo-dev.properties" file. The directory structure of the file is shown in the following figure:
The contents of jdbc-bainuo-dev.properties file are as follows:
application. The contents of the properties file are as follows:
In the above configuration file, spring profiles. The active property configures that the currently used environment is dev. spring. datasource. shareniu. URL is just a common attribute and has no special meaning.
Start writing the startup class of springboot as follows:
Run the above code, and the program output is as follows:
Strange, why is the URL empty? The propertysource annotation has already loaded the jdbc-bainuo-dev.properties file into the current environment? Let's try spring. Com in jdbc-bainuo-dev.properties datasource. shareniu. Whether the URL property can be obtained, and then verify from the side that the propertysource annotation has loaded the jdbc-bainuo-dev.properties file into the current environment.
Modify the code of the above startup class as follows:
Run the above code, and the program output is as follows:
From the above code, we can see that propertysource is indeed effective. So how do we put spring datasource. shareniu. Is the URL attribute value automatically injected into the URL attribute in the customerdatasourceconfig1 class?
1.3. The propertysource annotation reads the specified file and injects properties into the configuration class
Spring provides the @ value annotation, which is used to read out the property values in the configuration file and set them into the corresponding properties. Here we learn how to use the @ value annotation. Similarly, take the above two classes as examples for detailed description. First, you need to modify the customerdatasourceconfig1 class. The modification part is as follows:
In the above class, the @ value annotation is added in the URL field, and the spel expression is specified as ${spring. Datasource. Sharon. URL}. Run the springboot startup class again, and the output of the console is Sharon. It indicates that the attribute value can be injected through @ value. However, the @ value annotation method is not very friendly. When there are a large number of properties to be configured in the project, we need to add @ value annotations to the class fields one by one. This is really laborious, but we can solve this problem through the @ configurationproperties annotation provided by springboot.
1.4. Configurationproperties annotation usage
@Configurationproperties are class level annotations, which are used as follows:
In the above code, the configurationproperties annotation is added to the customerdatasourceconfig1 class, and the prefix of the property is spring datasource. shareniu。 In this way, when processing, springboot will scan all fields in the current class, find and assemble properties. For example, if we configure prefix = "spring. Datasource. Sharon", there is a URL field in the customerdatasourceconfig1 class, the matching attribute of the URL field is prefix + field = spring datasource. shareniu. url 。
That's not just a question? What if the specified field does not find the attribute? This can be configured as follows:
Ignoreunknownfields: ignore unknown fields.
Ignoreinvalidfields: whether to ignore fields that failed validation. How do you understand this? For example, if we configure a variable of string type in the configuration file, and the field in the class is int type, an error will be reported. If this situation can be tolerated, we need to configure the attribute value to true. The default value of this parameter is false.
This article will stop here for the time being. In subsequent articles, we will explain how the @ propertysource annotation can read configuration files in different environments. Files in different environments can be dynamically switched for reading. Propertysource is not supported by default, so we need to extend the source code corresponding to the annotation.
summary
The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.