0

I am a beginner for Spring, and I am learning Resource.

package com.smart.beanfactory;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class BeanLifeCycle {
    private static void LifeCycleInBeanFactory() {
        Resource res = new ClassPathResource("classpath:com/smart/beanfactory/beans.xml");
        try {
            System.out.println(res.getURL());
        } catch (Exception e) {
            System.out.println(e.fillInStackTrace());
        }
    }

    public static void main(String[] args) {
        LifeCycleInBeanFactory();
    }
}

I write the above simple code just to use the classpath to load the .xml file.

Below is the hierarchy of my project: enter image description here

Then I tried the absolute file path in Resource res = new ClassPathResource("file:/Users/haoxu/Documents/Code/Java/anotherchapter4/src/main/resources/com.smart/beanfactory/beans.xml"); But that also cannot work. It just told me "java.io.FileNotFoundException: class path resource [Users/haoxu/Documents/Code/Java/anotherchapter4/src/main/resources/com.smart/beanfactory/beans.xml] cannot be resolved to URL because it does not exist "

-----------update--------

I modified my code to:

package com.smart.beanfactory;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;

public class BeanLifeCycle {
    private static void LifeCycleInBeanFactory() {
        Resource res = new ClassPathResource("/com/smart/beanfactory/beans.xml");
        try {
            System.out.println(res.getURL());
        } catch (Exception e) {
            System.out.println(e.fillInStackTrace());
        }
    }

    public static void main(String[] args) {
        LifeCycleInBeanFactory();
    }
}

But I still get "java.io.FileNotFoundException: class path resource [com/smart/beanfactory/beans.xml] cannot be resolved to URL because it does not exist"

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
HAO
  • 89
  • 9
  • have you tried `"com.smart.beanfactory/beans.xml"` like it shows in the project tree? – RickyM Jan 04 '18 at 04:52
  • I tried it but it cannot work. java.io.FileNotFoundException: class path resource [com.smart.beanfactory/beans.xml] cannot be resolved to URL because it does not exist – HAO Jan 04 '18 at 04:54
  • Maybe it's the directory, instead of the com.smart.beqnfactory you should use a plain folder named xml or something then in the resource creation use "/xml/beans.xml". – RickyM Jan 04 '18 at 05:44

3 Answers3

2

The prefix classpath: is not required for ClassPathResource

amdg
  • 2,211
  • 1
  • 14
  • 25
  • agree. See the explanation on https://stackoverflow.com/questions/36588915/how-spring-classpathresource-is-working – Alex M981 Jan 04 '18 at 04:46
  • 1
    according to your screenshot there is no beans.xml file in classpath. See under "target/..." dir. beans.xml file must appear there automatically when you run your app – Alex M981 Jan 04 '18 at 05:06
  • Yes. My IDE seems does not put the .xml file to the target/ dir. I do it by myself and then the code works! – HAO Jan 04 '18 at 05:10
  • The target directory is the result of the build, you shouldn't be adding files to it. – RickyM Jan 04 '18 at 05:52
0

try the below line with your code

Resource res = new ClassPathResource("/resources/com/smart/beanfactory/beans.xml");
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
  • still get java.io.FileNotFoundException: class path resource [resources/com/smart/beanfactory/beans.xml] cannot be resolved to URL because it does not exist – HAO Jan 04 '18 at 04:52
  • It should work have included "/" like "/resources/com/smart/beanfactory/beans.xml" this? – Sasikumar Murugesan Jan 04 '18 at 04:59
0

I copy and paste the file "beans.xml" to the directory "target/classes/com/smart/beanfactory/". The hierarchy is like below enter image description here

My code is like below:

package com.smart.beanfactory;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;

public class BeanLifeCycle {
    private static void LifeCycleInBeanFactory() {
        Resource res = new ClassPathResource("com/smart/beanfactory/beans.xml");
        try {
            System.out.println(res.getURL());
        } catch (Exception e) {
            System.out.println(e.fillInStackTrace());
        }
    }

    public static void main(String[] args) {
        LifeCycleInBeanFactory();
    }
}

The result is "file:/Users/haoxu/Documents/Code/Java/anotherchapter4/target/classes/com/smart/beanfactory/beans.xml "

This is actually the path of the file. But it is in the target directory. I think that is really strange.

HAO
  • 89
  • 9