First, what isResource?

When we learned about Spring, we always declared beans in XML and then retrieved them through the following code snippet. The code is as follows:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Context {

  public static void main(String[] args) {
    
    ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring.xml"); System.err. Println (ctx.getbean (CTX."stu").toString()); // get a bean from config file and print it}}Copy the code

The first line of code loads a Resource file, spring.xml, which is a type of Resource in Spring. In this way, Spring calls all kinds of files, binary streams, resources, but to Spring developers, resources are mostly XML files.

Resource is an interface that defines a number of file manipulation policies, such as

  • Exists (): checks whether a resource exists
  • GetURL (): URL to get the resource
  • GetFilename (): Gets the name of the resource
  • . (There are so many ways, I won't list them all.)

The Resource interface is an abstraction of a concrete Resource access policy and is implemented by all Resource access classes. The Resource interface itself does not provide implementation logic for accessing any of the underlying resources. Spring will provide different Resource implementation classes for different underlying resources, and each implementation class is responsible for different Resource access logic.

One of the most important aspects of Spring Resource design, and one of the most beautiful aspects of Spring Resource design, is that Spring uses the policy pattern for handling Resource resources, which is why I call the methods in the Resource interface policies above!

If you do not know the Java strategy pattern, please learn by yourself!

Second,Spring ResourceInterface class structure diagram

To better understand this, I’ll put up a UML diagram of the policy pattern, which is not covered in this article.

All right! Now that the picture is up, how does the strategy pattern manifest itself in the Resource architecture?

  • 1.ResourceThe interface is equivalent to oursstrategy
  • 2. All of the following implementation classes, includingAbstractResourceThe inner is our concreteStrategy implementation
  • 3, You may ask, then ourcontextWhere are the characters? Ha ha, business can not forget our old Ben ah! It is our IOC containerApplicationContextIt is by far the most decisive boss in our strategy model

As for why the container can handle our Resource, I’ll talk about that later!

Obviously, Resource is the highest abstraction of a Resource. Usually our applications don’t load our XML files from the classpath. We can also get a Resource from a URL,URI,InputStream, etc. This is where Spring decouples how resources are fetched from their definition!

Three,ResourceLoaderinterface

  • ResourceLoaderAn instance of the interface implementation class can get a Resource instance.

In the ResourceLoader interface there are the following methods:

  • Resource getResource(String location): This interface contains only this method, which is used to return aResourceInstance.

The implementation classes of ApplicationContext all implement the ResourceLoader interface, which in turn returns a Resource instance, so ApplicationContext can be used to get the Resource instance directly, That’s why containers can handle resources!

Four,The strategy patternWhat are the advantages of using it in Spring

When a Spring application needs to access a Resource, it doesn’t actually use the Resource implementation class directly. Instead, it calls the getResource() method of the ApplicationContext instance to get the Resource. The ApplicationContext will be responsible for selecting the implementation class of the Resource, that is, determining the specific Resource access policy, thus separating the application from the specific Resource access policy.

Since there are so many advantages, the following is the first paragraph of the above to explain our code, why use a ClassPathXmlApplicationContext loading resource for this class?

As we all know, there are many implementations in the container ApplicationContext, Such as FileSystemXmlApplicationContext ClassPathXmlApplicationContext XmlWebApplicationContext, and the way we load the configuration file is usually use them to load, In the Spring ApplicationContext implementation class just as its name implies is corresponding to the us some of the Resource interface implementation strategy, such as ClassPathResource FileSystemResource, etc.