Spring Boot
application.properties
resources folder
file path configuration
Spring Framework

specify files in resources folder in spring application.properties file

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Spring Boot, files under src/main/resources are packaged on the classpath. A common mistake is referencing them as filesystem paths in application.properties, which works in IDE runs but fails in packaged JAR deployments. The robust approach is to reference classpath resources and load them through Spring abstractions.

If your property configuration needs a file location, decide whether that file is bundled with the app (classpath) or external runtime config (filesystem). Mixing these models causes deployment bugs.

Core Sections

1. Use classpath-prefixed properties for bundled resources

properties
app.template.path=classpath:templates/report.txt
app.rules.path=classpath:config/rules.json

Then inject as Spring Resource:

java
1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.core.io.Resource;
3
4@Service
5public class TemplateService {
6    @Value("${app.template.path}")
7    private Resource templateResource;
8
9    public String loadTemplate() throws IOException {
10        try (InputStream in = templateResource.getInputStream()) {
11            return new String(in.readAllBytes(), StandardCharsets.UTF_8);
12        }
13    }
14}

This works in IDE, tests, and fat JAR packaging.

2. Externalize with filesystem paths when needed

For environment-specific files, use configurable external locations:

properties
app.override.path=file:/etc/myapp/override.yaml

You can override via environment variables:

bash
APP_OVERRIDE_PATH=file:/opt/config/override.yaml

This keeps sensitive or mutable config outside artifacts.

3. Use ResourceLoader for dynamic resource resolution

java
1@Autowired
2private ResourceLoader resourceLoader;
3
4public String read(String location) throws IOException {
5    Resource resource = resourceLoader.getResource(location);
6    try (InputStream in = resource.getInputStream()) {
7        return new String(in.readAllBytes(), StandardCharsets.UTF_8);
8    }
9}

With this pattern, the same code can read classpath: and file: locations.

4. Verify packaging behavior in tests

Add integration tests that run from packaged artifact context to catch path mistakes early.

java
1@SpringBootTest
2class ResourcePathTest {
3    @Autowired TemplateService service;
4
5    @Test
6    void templateLoads() throws Exception {
7        assertFalse(service.loadTemplate().isBlank());
8    }
9}

5. Keep path properties clear and documented

Use explicit names like app.resource.* for classpath and app.file.* for external paths. This avoids ambiguity across environments.

Common Pitfalls

  • Referring to src/main/resources/... directly in properties as if runtime can access source tree.
  • Using filesystem paths for bundled files and breaking JAR deployments.
  • Reading resources with new File(...) instead of Spring Resource APIs.
  • Mixing external and classpath semantics in one property without clear prefixing.
  • Skipping packaged-run tests and discovering path failures only in production.

Summary

When specifying resources in Spring properties, use classpath: for bundled files and file: for external runtime files. Load through Spring Resource abstractions so code remains deployment-independent. Test packaged execution paths and keep property naming explicit about source type. This approach prevents environment-specific path bugs and keeps configuration behavior predictable across local, CI, and production deployments.

To make this guidance robust in day-to-day engineering work, treat it as an executable checklist instead of one-time reading material. Capture the expected environment, dependency versions, runtime flags, and validation commands in your repository so every contributor can reproduce the same behavior from a clean setup. This is especially important when onboarding new developers, rotating on-call ownership, or debugging incidents under time pressure. Documentation that includes concrete commands, expected outputs, and failure interpretation prevents repeat confusion and shortens recovery time.

It is also worth adding at least one automated guardrail in CI that validates the highest-risk assumption described in the article. Depending on the topic, that guardrail may be a smoke test, policy check, schema validation, benchmark threshold, import check, or integration assertion against a minimal fixture. The goal is to fail fast when environment drift or configuration changes reintroduce old errors. Teams that convert troubleshooting knowledge into small, repeatable checks reduce operational noise and keep this class of issue from returning every sprint.

As a final hardening step, schedule a periodic verification run that executes the documented checks in a fresh environment image. This catches slow drift in platform defaults, dependency transitive updates, and infrastructure policies that may otherwise remain invisible until production rollout.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.