Method of reading configuration file in Src / main / resources directory in Maven project
In the development of Maven project, what should I do when I need to read the configuration file under Src /?
Let's assume that there is a file named Kafka Properties configuration file (why use kafka.properties, because this is a problem encountered when doing Kafka project. I found a lot of information on the Internet. I just want to be a porter and sort it out according to my own understanding)
1. Read in Java class
If the configuration file is not in Src / main / resources directory, you can use it directly
When the configuration file is placed in the Src / main / resources directory, only class can be used Getresourceasstream() method to load
At this time, the parameter path in the getresourceasstream (string name) method is written as follows:
1). If it is written as "Kafka. Properties", you will find it in the same directory as the class file of the current class (but obviously no one will put the configuration file in this location in a normal project).
2). If it is written as "/ Kafka. Properties", go to the classes directory of the whole project to find it, that is, target / classes
2. Read from the XML file of spring framework
First, let's write a configuration file Kafka properties
There are two methods to call
1). First, it can be configured in the bean of spring
Here, you can also configure multiple values in the list tag, so that you can read one or more configuration files in the bean.
In this way, the parameters in the configuration file can be successfully called in the following beans. The annotated property above has the same effect as the annotated line
2). Alternatively, the following methods can be used
Directly configure the context: Property placeholder in the spring configuration file. Multiple configuration files can be separated by commas, for example
The method called follows 1) As in, it won't be repeated here
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.