These reviews

Spring IoC’s refresh function Spring IoC’s refresh function Spring IoC’s refresh function Spring IoC’s refresh function We pushed too hard. We need to lay down some basics. For example, while looking at the refresh function source code, I wonder if you noticed that all Spring resources are accessed through resources. Spring converts all resources into specific Resource objects before they are accessed. So what is this Resource?

Resource

The purpose of the Resource

Typically, we start the IoC container like this.

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/context.xml");
Copy the code

Even without looking at the source code, we know that for meta-INF /context.xml, there must be code inside Spring to get information about the actual XML file based on that address.

So how does Spring do this?

The answer is to use Resource. All resources in Spring, including files, ClassPath, and urls, are converted into resources for external services.

For example, the simplest one above is converted this way during the refresh function.

Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
Copy the code

Yeah, obviously, although we don’t know what resource Reader is. However, this code is also very easy to understand, converting the XML file under location to a Resource.

Why have Resource

Because the traditional way to access resources in Java is abstracted through urls. If you don’t remember URL abstraction, check out your college Java basics textbook. This is how it’s usually used.

     URL url = new URL("http://www.runoob.com/index.html?language=cn#j2se");
     System.out.println("URL is:" + url.toString());
Copy the code

What if you want to get a file relative to the classpath? What does traditional Java do? The first step is to get the URL, and then you can get the file that exists in that URL. You can then wrap the generation with a File object.

        URL url = LearnResource.class.getClassLoader().getResource("meta-inf/context.xml");
        System.out.println(url.getFile());
        File file = new File(url.getFile());
        System.out.println(file.exists());
Copy the code

So, using urls directly to get resources under the CLASspath is a bit more complicated.

In addition, there are no basic methods for URL abstraction. For example, there are no methods to check whether the current resource exists or whether the current resource is readable.

Therefore, Spring uses its own Resource to encapsulate access to the files below the above classpath.

With Resource, how can you access the corresponding file resources? One line. Isn’t that easy?

ClassPathResource classPathResource = new ClassPathResource("meta-inf/context.xml");
Copy the code

The structure of the Resource

Spring has designed the Resource interface to expose all the ways in which resources are accessed. The Resource inherits InputSteamSource to provide the getInputStream method to get the InputStream. Also provides getFile, exists, isReadable getURL, etc.

ClassPathResource, FileSystemResource, ClassPathResource, FileSystemResource, ClassPathResource, FileSystemResource, ClassPathResource, FileSystemResource

Finally, a Resource class diagram is attached.

Think of some additional points, some specific implementation of ClassLoader, you can think about it a little bit more, and we’ll talk about it later. This article will give a basic introduction to Resource.

About writing

From now on, I will write an article here every day, with no limit on subject matter, content or word count. Try to put your daily thoughts into it.

If this article has helped you, give it a thumbs up and even better follow it.

If none of these are available, write down what you want to say when you finish reading? Effective feedback and your encouragement are the biggest help to me.

And I’m going to pick up my blog. Welcome to visit and eat watermelon.

I’m shane. Today is August 17, 2019. The twenty-fourth day of the hundred day writing project, 24/100.