Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use as below

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...