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:
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"