<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Spring `ResourceLoaderAware` - 在 Spring 中讀取文件 > 原文: [https://howtodoinjava.com/spring-core/spring-resource-loader-aware/](https://howtodoinjava.com/spring-core/spring-resource-loader-aware/) 了解將**資源或文件**(例如,文本文件,XML 文件,屬性文件或圖像文件)加載到 Spring 應用程序上下文中的不同方法。 **Spring `ResourceLoader`**為我們提供了統一的`getResource()`方法,以便我們通過資源路徑檢索外部[資源](https://howtodoinjava.com/java/io/read-file-from-resources-folder/)。 ## 1\. 資源接口表示資源 [資源](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/Resource.html)是 Spring 中用于表示外部資源的通用接口。 Spring 為`Resource`接口提供了以下 6 種實現。 1. `UrlResource` 2. `ClassPathResource` 3. `FileSystemResource` 4. `ServletContextResource` 5. `InputStreamResource` 6. `ByteArrayResource` 我們可以指定不同的前綴來創建路徑以從不同位置加載資源。 | 前綴 | 示例 | 說明 | | --- | --- | --- | | `classpath:` | `classpath:com/myapp/config.xml` | 從類路徑加載。 | | `file:` | `file:///data/config.xml` | 從文件系統作為`URL`加載。 | | `http:` | `https://myserver/logo.png` | 加載為`URL`。 | | (沒有) | `/data/config.xml` | 取決于基礎的`ApplicationContext`。 | ## 2\. `ResourceLoader` 它用于加載資源(例如類路徑或文件系統資源)。 它有兩種方法: `ResourceLoader methods` ```java //Expose the ClassLoader used by this ResourceLoader. ClassLoader getClassLoader() //Return a Resource handle for the specified resource location. Resource getResource(String location) ``` `getResource()`方法將根據資源路徑決定實例化哪個`Resource`實現。 要獲取`ResourceLoader`的引用,請實現`ResourceLoaderAware`接口。 `How to get resource` ```java Resource banner = resourceLoader.getResource("file:c:/temp/filesystemdata.txt"); ``` ## 3\. 使用`ApplicationContext`加載資源 在 Spring 中,所有應用程序上下文都實現`ResourceLoader`接口。 因此,所有應用程序上下文都可用于獲取資源實例。 要獲取`ApplicationContext`的引用,請實現`ApplicationContextAware`接口。 `How to get resource` ```java Resource banner = ctx.getResource("file:c:/temp/filesystemdata.txt"); ``` ## 4\. 使用`ResourceLoaderAware`加載資源 為了演示下面的各種示例,我將一個具有相同名稱的文件放置在不同的位置,并且我將演示如何加載每個文件。 `CustomResourceLoader.java`編寫如下,將已加載的資源文件的內容打印到控制臺中。 `CustomResourceLoader.java` ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; public class CustomResourceLoader implements ResourceLoaderAware { private ResourceLoader resourceLoader; public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public void showResourceData() throws IOException { //This line will be changed for all versions of other examples Resource banner = resourceLoader.getResource("file:c:/temp/filesystemdata.txt"); InputStream in = banner.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); while (true) { String line = reader.readLine(); if (line == null) break; System.out.println(line); } reader.close(); } } ``` 該文件的`applicationContext.xml`文件條目如下: `applicationContext.xml` ```java <bean id="customResourceLoader" class="com.howtodoinjava.demo.CustomResourceLoader"></bean> ``` 要測試`CustomResourceLoader` bean 并調用`showResourceData()`方法,已使用以下代碼: `Main.java` ```java @SuppressWarnings("resource") public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); CustomResourceLoader customResourceLoader = (CustomResourceLoader) context.getBean("customResourceLoader"); customResourceLoader.showResourceData(); } ``` ![spring-load-external-resource-example](https://img.kancloud.cn/50/4c/504c095d6b96ff1726859fe514c6d906_336x268.jpg) > 由于我們正在通過 Spring 的資源加載器訪問資源,因此自定義資源加載器必須實現`ApplicationContextAware`接口或`ResourceLoaderAware`接口。 ## 5\. 加載外部資源 #### 5.1. 從應用程序根文件夾加載資源 要從應用程序文件夾加載文件,請使用以下模板: ```java Resource banner = resourceLoader.getResource("file:data.txt"); ``` #### 5.2. 從類路徑加載資源 要從類路徑加載文件,請使用以下模板: ```java Resource banner = resourceLoader.getResource("classpath:classpathdata.txt"); ``` #### 5.3. 從文件系統加載資源 要從應用程序文件夾外部的文件系統加載文件,請使用以下模板: ```java Resource banner = resourceLoader.getResource("file:c:/temp/filesystemdata.txt"); ``` #### 5.4. 從 URL 加載資源 要從任何 URL 加載文件,請使用以下模板: ```java Resource banner = resourceLoader.getResource("//howtodoinjava.com/readme.txt"); ``` 以上所有示例將從其位置加載資源文件,您可以按需要使用它們。 ## 6\. 如何注入外部文件 在上面的示例中,我們已經在`CustomResourceLoader`中對資源名稱進行了硬編碼,很多人可能不喜歡它,并且希望通過上下文文件對其進行配置。 使用下面的代碼模板**可以配置外部資源名稱**。 `beans.xml` ```java <bean id="customResourceLoader" class="com.howtodoinjava.demo.CustomResourceLoader"> <property name="resource"> <value>classpath:classpathdata.txt</value> <!-- or --> <value>file:data.txt</value> </property> </bean> ``` `CustomResourceLoader`如下所示: `CustomResourceLoader.java` ```java public class CustomResourceLoader { private Resource resource; public Resource getResource() { return resource; } public void setResource(Resource resource) { this.resource = resource; } } ``` 上下文初始化后,資源將注入到`CustomResourceLoader`的`resource`屬性中。 在 spring boot `ResourceLoader`示例中可以使用相同的代碼。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看