Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Spring ClassPathResource: with classpath: or classpath*: and leading / or not?

Tags:

java

spring

I having file in the location

--src
  --> main
   --> config
    --> application
     --> context
      --> reference
       --> user
        --> user.xml

where

    --src
      --> main
       --> config

is in the classpath. Now I am trying to access the file using

Resource resource = new ClassPathResource("classpath**:/application/context/references/user/user.xml");
File file = resource.getFile();

But I getting FileNotFoundException, I tried with

Resource resource = new ClassPathResource("classpath:/application/context/references/user/user.xml");
File file = resource.getFile();

too, but still I getting the exception. Can someone help me to understand the working of ClassPathResource and right solution?

like image 954
Suganthan Madhavan Pillai Avatar asked Apr 13 '16 04:04

Suganthan Madhavan Pillai


People also ask

What is the use of ClassPathResource in spring?

ClassPathResource ClassPathResource is a Resource implementation for class path resources. It supports resolution as java. io. File if the class path resource resides in the file system, but not for resources in a JAR.

What is the classpath in spring boot project?

It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable . jar , some physical file system location used in the classpath (with java 's -cp option), etc.

What does classpath mean in spring?

This special prefix specifies that all classpath resources that match the given name must be obtained (internally, this essentially happens via a ClassLoader. getResources(...) call), and then merged to form the final application context definition. So classpath: starts at the root of your classpath.


1 Answers

Use as below

Resource resource = new ClassPathResource("/application/context/references/user/user.xml");
File file = resource.getFile();
like image 189
KayV Avatar answered Sep 30 '22 15:09

KayV