Spring Resource

Outline, this article mainly introduces Spring Resource related

One: Resource related

Two: ResourceLoader related

Three: Resource ant loading mode related

 

One: Resource related

    First, let's take a look at the commonly used methods of Resource:

    



 
 

    Take a look at the class diagram of Resource:


 

From the above class diagram, we introduce some commonly used classes

UrlResource:

     An implementation class for accessing network resources. Start with the following http: ftp: file: such as: http://myserver/logo.png

ClassPathResource:

     Access the implementation class of the resource in the class loading path. The effect is similar to this.getClass().getResource("/").getPath();

FileSystemResource:

     An implementation class for accessing resources in the file system. Such as: file:/data/config.xml

ServletContextResource:

     Access the implementation class relative to the resource in the ServletContext path, the path must start with "/", such as: /WEB-INF/web.xml

     The effect is similar to request.getServletContext().getRealPath("");

InputStreamResource:

      An implementation class to access input stream resources.

ByteArrayResource:

      An implementation class for accessing byte array resources.

 

Two: ResourceLoader related

ResourceLoader contains a Resource object, so its main function is the getResource() function.


    It can be seen that ResourcePatternResolver encapsulates a layer on ResourceLoader, supports multi-resource loading, and it is worth mentioning that it also supports paths in Ant format.

 


 
 

 Implementation class of ResourcePatternResolver:

     

Three: Resource ant loading mode related

    Ant path wildcard supports "?", "*", "**", note that wildcard matching does not include the directory separator "/": 

    “?”:匹配一个字符,如“config?.xml”将匹配“config1.xml”;

    “*”:匹配零个或多个字符串,如“cn/*/config.xml”将匹配“cn/javass/config.xml”,但不匹配匹配“cn/config.xml”;而“cn/config-*.xml”将匹配“cn/config-dao.xml”;

     “**”:匹配路径中的零个或多个目录,如“cn/**/config.xml”将匹配“cn /config.xml”,也匹配“cn/javass/spring/config.xml”;而“cn/javass/config-**.xml”将匹配“cn/javass/config-dao.xml”,即把“**”当做两个“*”处理。

    “classpath*:”用于加载所有匹配的类路径Resource

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327038908&siteId=291194637