Avoid classpath wildcard in Spring resource paths

ID

java.spring_no_wildcard_classpath_resource

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

best-practice, efficiency, reliability, spring

Description

Reports string literals containing the classpath*: prefix.

Rationale

The classpath*: wildcard resource pattern forces the Spring resource loader to scan every JAR and directory on the classpath for matching resources. This is significantly slower than an explicit classpath: path and can produce fragile results that vary depending on the classpath order and contents, especially in fat-JAR or container environments.

// Bad - scans all JARs
@Value("classpath*:config/*.properties")
private Resource[] configs;

Remediation

Use explicit classpath: paths or enumerate specific resources.

// Good - explicit path
@Value("classpath:config/application.properties")
private Resource config;