In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use the Spring class to read the configuration file". In the daily operation, I believe that many people have doubts about how to use the Spring class to read the configuration file. The editor consulted all kinds of information and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubt of "how to use the Spring class to read the configuration file". Next, please follow the editor to study!
1. Preface
It is common to read the configuration or static files in the project during development. I believe many students are looking for the following examples to load and read files from the Internet.
Use ClassLoader to obtain the classpath path:
ClassLoader loader = Thread.currentThread () .getContextClassLoader (); String classPath = loader.getResource (") .getPath ()
Or use the Class object to get the absolute path or classpath path:
String absolutepath = this.getClass () .getResource (") .getPath (); String classPath = this.getClass () .getResource (" / ") .getPath ()
Then read the file in the spliced file path. Why am I so clear? Because I've done the same thing. But now I don't do that anymore, because I found that the Spring framework provides a more useful way to load and read files.
2. ClassPathResource
Generally speaking, the configuration files and static resources of our project are placed in the resources directory. At this point, we can access it through the ClassPathResource provided by Spring. For example, I want to load WeChat Pay's certificate in my latest WeChat Pay SDK project:
The classpath of Wechat certificate is wechat/apiclient_cert.p12
The corresponding reading method is:
ClassPathResource resource = new ClassPathResource (keyPath); InputStream is = resource.getInputStream ()
Then you can read the contents of the configuration file from the stream, which is very simple.
There are other methods that are also useful, such as determining whether a file exists, getting the file's File object, and so on.
ClassPathResource's pit
When it comes to getting the getFile () method of the file object, there is a small hole here. It doesn't work when you use this method to read the classpath file in the Jar package.
After debugging the code, it is found that the address loaded is a Jar file protocol address, which is similar to this format, jarposition filebank, xxxmax, xxx. jarcode, xxx.
The getFile () method only supports the vfs protocol header of JBoss and the file protocol header of traditional file systems. So getInputStream () is recommended.
At this point, the study on "how to use Spring classes to read configuration files" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.