ZetCode

Spring ClassPathResource tutorial

last modified October 18, 2023

Spring ClassPathResource tutorial shows how to read resources with a ClassPathResource in a Spring application.

Spring is a popular Java application framework for creating enterprise applications.

Spring ClassPathResource

ClassPathResource allows to obtain resources from a Java classpath.

Spring ClassPathResource example

The application reads text data from a file located in the Java classpath.

src
├───main
│ ├───java
│ │ └───com
│ │ └───zetcode
│ │ │ Application.java
│ │ └───service
│ │ ReadWordsService.java
│ └───resources
│ logback.xml
│ my-beans.xml
│ words.txt
└───test
 └───java

This is the project structure.

pom.xml
<?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>classpathres</artifactId>
 <version>1.0-SNAPSHOT</version>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <maven.compiler.source>17</maven.compiler.source>
 <maven.compiler.target>17</maven.compiler.target>
 <spring-version>5.3.23</spring-version>
 </properties>
 <dependencies>
 <dependency>
 <groupId>ch.qos.logback</groupId>
 <artifactId>logback-classic</artifactId>
 <version>1.4.0</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>3.1.0</version>
 <configuration>
 <mainClass>com.zetcode.Application</mainClass>
 </configuration>
 </plugin>
 </plugins>
 </build>
</project>

In the pom.xml file, we have basic Spring dependencies spring-core and spring-context and logging logback-classic dependency.

The exec-maven-plugin is used for executing Spring application from the Maven on the command line.

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

In the my-beans.xml file, we configure the readWordsService bean. It becomes a Spring managed bean.

resources/words.txt
sky
blue
mountain
fresh
cloud
rock
water
melon

The resources directory is included in the classpath. The application reads words from the words.txt file.

resources/logback.xml
<?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} [%thread] %blue(%-5level) %magenta(%logger{36}) - %msg %n
 </Pattern>
 </encoder>
 </appender>
 <root>
 <level value="INFO" />
 <appender-ref ref="consoleAppender" />
 </root>
</configuration>

The logback.xml is a configuration file for the Logback logging library.

com/zetcode/service/ReadWordsService.java
package com.zetcode.service;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
public class ReadWordsService {
 public List<String> readWords() throws IOException {
 var res = new ClassPathResource("words.txt");
 var myFile = res.getFile();
 var lines = Files.readAllLines(myFile.toPath());
 return lines;
 }
}

The ReadWordsService reads the words into a list and returns the list to the client.

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

The ClassPathResource is used to locate the text file.

com/zetcode/Application.java
package com.zetcode;
import com.zetcode.service.ReadWordsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.GenericXmlApplicationContext;
import java.io.IOException;
public class Application {
 private static final Logger logger = LoggerFactory.getLogger(Application.class);
 public static void main(String[] args) throws IOException {
 var ctx = new GenericXmlApplicationContext("my-beans.xml");
 var wordsService = (ReadWordsService) ctx.getBean("readWordsService");
 var words = wordsService.readWords();
 words.forEach(word -> logger.info(word));
 ctx.close();
 }
}

This is the main application class.

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

We retrieve the readWordsService bean from the container and call its readWords method. The words are printed to the console.

$ mvn -q exec:java
00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - sky
00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - blue
00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - mountain
00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - fresh
00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - cloud
00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - rock
00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - water
00:25:49.008 [com.zetcode.Application.main()] INFO com.zetcode.Application - melon

We run the application.

In this article we have worked with Spring's ClassPathResource.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Spring tutorials.

AltStyle によって変換されたページ (->オリジナル) /