Explain the five ways to let you read the contents of the properties file in Java is no longer a problem

1、 Background

Recently, in the process of project development, some custom variables need to be defined in the properties file for Java programs to dynamically read and modify variables, and there is no need to modify the code. I took this opportunity to sort out and analyze the way to read the contents of the properties file through Java program in the project of integrated development of Spring + spring MVC + mybatis. I'll share it with you first.

2、 Project environment introduction

3、 Five implementation methods

Mode 1 Load the configuration file JDBC through context: Property placeholder Content in properties

The above configuration is equivalent to the following configuration, which simplifies the following configuration

Note: in this way, if you are in spring MVC XML file has the following configuration, you must not lack the following red part, about its function and principle

Mode 2 Annotation injection is mainly used to inject the corresponding value value in the properties file in Java code

Mode 3 Use the util: properties tag to expose the contents of the properties file

Note: to use the above line of configuration, you need to use spring Dao The header of the XML file declares the following red part

Mode 4 Through propertyplaceholderconfigurer, the properties are exposed to the properties of the self-defined subclass when the context is loaded for use in the program

The custom class propertyconfigurator is declared as follows:

Usage: use @ Autowired annotation injection in the class to be used.

Mode 5 Customize the tool class propertyutil, read the properties file in the static code block of this class, and save the contents in the static property for use by other programs

Note: in this way, when the class is loaded, it will automatically read the content of the configuration file at the specified location and save it to the static properties. It is efficient and convenient. It can be loaded once and used many times.

4、 Precautions and suggestions

Among the above five methods, the first three are relatively rigid, and if you want to use them in beans with @ controller annotation, you need to use spring MVC in the spring MVC configuration file XML. If you want to use it in beans with @ service, @ repository and other non @ controller annotations, you need to add spring. XML in the spring configuration file XML.

I personally recommend the fourth and fifth configuration methods. The fifth is the best. It doesn't even need to inject tool class objects. It directly calls static methods for acquisition, and only loads once, with high efficiency. Moreover, the first three methods are not very flexible, and the key value of @ value needs to be modified.

5、 Test to verify availability

1. First, we create propertiesservice

2. Create the implementation class propertiesserviceimpl

3. Controller class propertycontroller

4.jdbc. Properties file

5. Project result chart

6. Project Download: Demo http://xiazai.jb51.net/201612/yuanma/propertiesConfigurer_jb51.zip

7. Test results

The first way

The second way

The third way

The fourth way

The fifth way

6、 Summary

Through this combing and testing, we have understood the parent-child container relationship between spring and spring MVC, as well as the function and principle of the use default filters attribute, which is most easily ignored in context: component scan Tag Package scanning. It can better locate and quickly solve the problems encountered again. Anyway, great~~~

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>