<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Spring 資源教程 > 原文: [http://zetcode.com/spring/resource/](http://zetcode.com/spring/resource/) Spring `Resource`教程展示了如何使用`Resource`在 Spring 應用中使用各種資源。 Spring 是用于創建企業應用的流行 Java 應用框架。 ## Spring 資源 `Resource`從基礎資源的實際類型中抽象出來,例如文件或類路徑資源。 它可以用來標識本地或遠程資源。 Spring `ApplicationContext`包含`getResource()`方法,該方法返回指定資源類型的資源句柄。 它可以是類路徑,文件或 URL 資源。 ## Spring 資源示例 該應用使用 Spring 的`Resource`來讀取本地文件和遠程網頁。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ └───service │ │ MyService.java │ └───resources │ logback.xml │ words.txt └───test └───java ``` 這是項目結構。 `pom.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>resourceex</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <spring-version>5.1.3.RELEASE</spring-version> </properties> <dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <configuration> <mainClass>com.zetcode.Application</mainClass> </configuration> </plugin> </plugins> </build> </project> ``` 在`pom.xml`文件中,我們具有基本的 Spring 依賴項`spring-core`,`spring-context`和日志記錄`logback-classic`依賴項。 `exec-maven-plugin`用于在命令行上從 Maven 執行 Spring 應用。 `resources/logback.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <configuration> <logger name="org.springframework" level="ERROR"/> <logger name="com.zetcode" level="INFO"/> <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <Pattern>%d{HH:mm:ss.SSS} %blue(%-5level) %magenta(%logger{36}) - %msg %n </Pattern> </encoder> </appender> <root> <level value="INFO" /> <appender-ref ref="consoleAppender" /> </root> </configuration> ``` `logback.xml`是 Logback 日志庫的配置文件。 `resources/words.txt` ```java clean sky forest blue crystal cloud river ``` `words.txt`文件包含幾個單詞。 `com/zetcode/MyService.java` ```java package com.zetcode.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @Service public class MyService { private static final Logger logger = LoggerFactory.getLogger(MyService.class); @Autowired private ApplicationContext ctx; public void readWebPage() { var res = ctx.getResource("http://webcode.me"); try (var is = new InputStreamReader(res.getInputStream()); var bis = new BufferedReader(is)) { bis.lines().forEach(System.out::println); } catch (IOException ex) { logger.warn("{}", ex); } } public void readFile() { // var res = ctx.getResource("file:C:/Users/Jano/Documents/words.txt"); var res = ctx.getResource("classpath:words.txt"); try (var is = new InputStreamReader(res.getInputStream()); var bis = new BufferedReader(is)) { bis.lines().forEach(System.out::println); } catch (IOException ex) { logger.warn("{}", ex); } } } ``` `MyService`有兩種讀取網頁和本地文本文件的方法。 ```java @Autowired private ApplicationContext ctx; ``` 我們注入`ApplicationContext`。 我們使用其`getResource()`方法來獲取資源處理器。 ```java var res = ctx.getResource("http://webcode.me"); ``` 我們從網頁上獲得了`Resource`。 ```java // var res = ctx.getResource("file:C:/Users/Jano/Documents/words.txt"); var res = ctx.getResource("classpath:words.txt"); ``` 我們可以從絕對文件路徑或類路徑獲取`Resource`。 `com/zetcode/Application.java` ```java package com.zetcode; import com.zetcode.service.MyService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; @ComponentScan(basePackages = "com.zetcode") public class Application { private static final Logger logger = LoggerFactory.getLogger(Application.class); @Autowired private MyService myService; public static void main(String[] args) { var ctx = new AnnotationConfigApplicationContext(Application.class); var app = ctx.getBean(Application.class); app.run(); ctx.close(); } public void run() { myService.readWebPage(); myService.readFile(); } } ``` 這是主要的應用類。 ```java @Autowired private MyService myService; ``` 使用`@Autowired`將服務 bean 注入到類中。 ```java myService.readWebPage(); myService.readFile(); ``` 我們稱為`myService`方法。 在本教程中,我們展示了如何使用`Resource`來讀取本地文本文件和網頁。 您可能也對這些相關教程感興趣: [Spring `@Qualifier`注解教程](/spring/qualifier/), [Spring 單例范圍 bean](/spring/singletonscope/) , [Spring C-命名空間教程](/spring/cnamespace/), [Spring `BeanDefinitionBuilder`教程](/spring/beandefinitionbuilder/), [Spring bean 引用教程](/spring/beanreference/)和 [Java 教程](/lang/java/)。
                  <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>

                              哎呀哎呀视频在线观看