简体   繁体   中英

java spring framework code doesn't run

I have this code

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DrawingApp {
public static void main(String[] args) {


    ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

    Triangle triangle = (Triangle) context.getBean("triangle");


 triangle.draw();
   }
 }

and I got this error

Jan 17, 2017 11:14:48 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6576fe71: startup date [Tue Jan 17 23:14:48 EST 2017]; root of context hierarchy Jan 17, 2017 11:14:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [spring.xml] Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring.xml] cannot be opened because it does not exist at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApp licationContext.java:127) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at fahad.DrawingApp.main(DrawingApp.java:14) Caused by: java.io.FileNotFoundException: class path resource [spring.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResour ce.java:158) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) ... 13 more


by the way, the spring.xml file and .classpath are at the same folder,

so what should I have to do to run this code ?

Thanks

It has nothing to do with the .classpath file, spring is expecting to find the spring.xml file in the classpath, meaning in the directories where your class files are stored.

If you follow the maven conventions for laying out your project then you would put the spring.xml under src/main/resources, you can specify it as a source folder in eclipse. At any rate put the file in a source folder with your code. The ide will copy any files it finds there into your classpath.

To clearer picture, Please refer here - Class Path - Documentation, Java

The class path is the path that the Java runtime environment searches for classes and other resource files

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.

Also, Please refer ClassPathXmlApplicationContext . If you place inside src folder, it would be referred.

Need a tutorial???, try the source code here ClassPathXmlApplicationContext-Tutorial - it uses ClassPathXmlApplicationContext.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM