diff --git a/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java b/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java index be56956d68..4b96efc7d7 100644 --- a/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java @@ -121,18 +121,25 @@ public class ClassPathResource extends AbstractFileResolvingResource { /** * Return the path for this resource. *
If this resource was created using - * {@link ClassPathResource#ClassPathResource(String) ClassPathResource(String)}, - * {@link ClassPathResource#ClassPathResource(String, ClassLoader) - * ClassPathResource(String, ClassLoader)}, or + * {@link ClassPathResource#ClassPathResource(String) ClassPathResource(String)} + * or {@link ClassPathResource#ClassPathResource(String, ClassLoader) + * ClassPathResource(String, ClassLoader)}, the returned path is a + * {@linkplain StringUtils#cleanPath(String) cleaned} version of the + * absolute path supplied to the constructor, without + * a leading slash. + *
If this resource was created using * {@link ClassPathResource#ClassPathResource(String, Class) * ClassPathResource(String, Class)} with an absolute path, the returned path * is a {@linkplain StringUtils#cleanPath(String) cleaned} version of the - * absolute path supplied to the constructor. + * absolute path supplied to the constructor, with + * a leading slash. *
If this resource was created using * {@link ClassPathResource#ClassPathResource(String, Class) * ClassPathResource(String, Class)} with a relative path, the returned path * is a {@linkplain StringUtils#cleanPath(String) cleaned} version of the * relative path supplied to the constructor. + *
The path returned by this method cannot be reliably used with + * {@link ClassLoader#getResource(String)}. *
If you consistently need the absolute path, use * {@link #getAbsolutePath()} instead. * @see #getAbsolutePath() @@ -143,7 +150,9 @@ public class ClassPathResource extends AbstractFileResolvingResource { /** * Return the absolute path for this resource, as a resource path - * within the class path. + * within the class path without a leading slash. + *
The path returned by this method is suitable for use with + * {@link ClassLoader#getResource(String)}. * @since 6.0 * @see #getPath() */