原文: http://zetcode.com/spring/classpathresource/

Spring ClassPathResource教程显示了如何在 Spring 应用中使用ClassPathResource读取资源。

Spring 是用于创建企业应用的流行 Java 应用框架。

Spring ClassPathResource

ClassPathResource允许从 Java 类路径获取资源。

Spring ClassPathResource示例

该应用从 Java 类路径中的文件读取文本数据。

  1. src
  2. ├───main
  3. ├───java
  4. └───com
  5. └───zetcode
  6. Application.java
  7. └───service
  8. ReadWordsService.java
  9. └───resources
  10. logback.xml
  11. my-beans.xml
  12. words.txt
  13. └───test
  14. └───java

这是项目结构。

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.zetcode</groupId>
  8. <artifactId>classpathres</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <maven.compiler.source>11</maven.compiler.source>
  13. <maven.compiler.target>11</maven.compiler.target>
  14. <spring-version>5.1.3.RELEASE</spring-version>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>ch.qos.logback</groupId>
  19. <artifactId>logback-classic</artifactId>
  20. <version>1.2.3</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework</groupId>
  24. <artifactId>spring-context</artifactId>
  25. <version>${spring-version}</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework</groupId>
  29. <artifactId>spring-core</artifactId>
  30. <version>${spring-version}</version>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.codehaus.mojo</groupId>
  37. <artifactId>exec-maven-plugin</artifactId>
  38. <version>1.6.0</version>
  39. <configuration>
  40. <mainClass>com.zetcode.Application</mainClass>
  41. </configuration>
  42. </plugin>
  43. </plugins>
  44. </build>
  45. </project>

pom.xml文件中,我们具有基本的 Spring 依赖项spring-corespring-context和日志记录logback-classic依赖项。

exec-maven-plugin用于在命令行上从 Maven 执行 Spring 应用。

resources/my-beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean name="readWordsService" class="com.zetcode.service.ReadWordsService"/>
  7. </beans>

my-beans.xml文件中,我们配置readWordsService bean。 它成为 Spring 托管的 bean。

resources/words.txt

  1. sky
  2. blue
  3. mountain
  4. fresh
  5. cloud
  6. rock
  7. water
  8. melon

resources目录包含在类路径中。 该应用从words.txt文件中读取单词。

resources/logback.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <logger name="org.springframework" level="ERROR"/>
  4. <logger name="com.zetcode" level="INFO"/>
  5. <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
  6. <encoder>
  7. <Pattern>%d{HH:mm:ss.SSS} [%thread] %blue(%-5level) %magenta(%logger{36}) - %msg %n
  8. </Pattern>
  9. </encoder>
  10. </appender>
  11. <root>
  12. <level value="INFO" />
  13. <appender-ref ref="consoleAppender" />
  14. </root>
  15. </configuration>

logback.xml是 Logback 日志库的配置文件。

com/zetcode/service/ReadWordsService.java

  1. package com.zetcode.service;
  2. import org.springframework.core.io.ClassPathResource;
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.util.List;
  6. public class ReadWordsService {
  7. public List<String> readWords() throws IOException {
  8. var res = new ClassPathResource("words.txt");
  9. var myFile = res.getFile();
  10. var lines = Files.readAllLines(myFile.toPath());
  11. return lines;
  12. }
  13. }

ReadWordsService将单词读入列表,并将列表返回给客户端。

  1. var res = new ClassPathResource("words.txt");

ClassPathResource用于定位文本文件。

com/zetcode/Application.java

  1. package com.zetcode;
  2. import com.zetcode.service.ReadWordsService;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.context.support.GenericXmlApplicationContext;
  6. import java.io.IOException;
  7. public class Application {
  8. private static final Logger logger = LoggerFactory.getLogger(Application.class);
  9. public static void main(String[] args) throws IOException {
  10. var ctx = new GenericXmlApplicationContext("my-beans.xml");
  11. var wordsService = (ReadWordsService) ctx.getBean("readWordsService");
  12. var words = wordsService.readWords();
  13. words.forEach(word -> logger.info(word));
  14. ctx.close();
  15. }
  16. }

这是主要的应用类。

  1. var wordsService = (ReadWordsService) ctx.getBean("readWordsService");
  2. var words = wordsService.readWords();
  3. words.forEach(word -> logger.info(word));

我们从容器中检索readWordsService bean,并调用其readWords()方法。 文字将打印到控制台。

  1. $ mvn -q exec:java
  2. 00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - sky
  3. 00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - blue
  4. 00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - mountain
  5. 00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - fresh
  6. 00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - cloud
  7. 00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - rock
  8. 00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - water
  9. 00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - melon

我们运行该应用。

在本教程中,我们使用了 Spring 的ClassPathResource

您可能也对这些相关教程感兴趣:经典 Spring 应用中的JdbcTemplateSpring 单例范围 beanSpring 注入列表 XML 教程Spring BeanDefinitionBuilder教程Spring HikariCP 教程Java 教程