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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to read files through Spring". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Overview
In the daily business development of java, it is often possible to read some custom configurations or files. For example, public and private key files, some fixed dictionary files, and so on, are collectively referred to as Resource. In many cases, you write your own IO to read. You don't have to build wheels from today. Because Spring has its own resource loading function, there is even a very convenient way to inject read content into Spring bean. Next, let's take a look.
two。 Through the Resource interface
We can use the Resource interface to simplify the location of resource files. Spring helps us find and read resources using the resource loader, which decides which Resource implementation to choose based on the path provided.
2.1 implementation classes that use Resource
There are two implementation classes commonly used in the org.springframework.core.io.Resource interface: org.springframework.core.io.ClassPathResource is used to load resources under classpath. We directly read the springboot configuration file application.properties, which has been written into a configuration server.port=8080
Org.springframework.core.io.FileSystemResource is used to load system files, usually read through the absolute or relative path of the file. Let's try this with the above example. The difference is that this time we need the path to the file.
2.2 using ResourceLoader
If you need to use deferred loading in your business, we can use the class ResourceLoader:
We can also use @ Autowired to inject ResourceLoader into our bean:
According to the figure above, ApplicationContext inherits the ResourceLoader interface. So we can read resources through Spring bean injection.
This method of use looks more Spring, and then there are more Spring operations.
2.3 use @ Value annotations
We directly use the @ Value annotation to inject resources directly into Spring bean. And this kind of operation! I can't! I can't wait to write a few lines to try:
Perfect! The assertion is all correct! I don't know if I can read the picture. I think so. I'll leave it to you to find out for yourself. But is it over? Play something exciting. Why not just parse out and inject the object? SpEL (Spring Expression Language) can do this. Let's see. In order to show flexibility, we define our own parsing class here using SpEL direct parsing. We defined a utility class cn.felord.mapstruct.util.ResourceReader to read the configuration file
Both methods have the same effect, but show different approaches. The following code must make its appearance different:
Commented-out code also works, and it's easy to observe the differences between them.
This is the end of "how to read a file through Spring". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.