Home > Software design >  I am trying to run this first Spring program using XML and I am getting following error. I am novice
I am trying to run this first Spring program using XML and I am getting following error. I am novice

Time:01-16

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DrawingApp {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring.xml");
        Triangle triangle= (Triangle) applicationContext.getBean("triangle");
        triangle.draw();
    }
}

public class Triangle {
    public void draw()
    {
        System.out.println("Triangle Class");
    }
}

This is the Java program.

    <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <bean id="triangle" />
</beans>

This is the XML File. And When I am trying to run this program, I am getting the following error

14:32:12.644 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@721e0f4f 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:342) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:257) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:128) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:94) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:671) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:144) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:85) at practice.DrawingApp.main(DrawingApp.java:9) 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(ClassPathResource.java:199) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:333) ... 13 more

I have also used this code in XML

    <?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="triangle" />
</beans

Then also I am getting the same error

Guide me to do this spring programming perfectly.

CodePudding user response:

Your XML file is in a wrong directory. ClassPathXmlApplicationContext searches for it on the Java classpath. Java classpath purpose is to locate available compiled class files, but it became very common to place runtime configuration files there too.

If you use Maven to build your project, you can place your Spring.xml file into the src/main/resources. Maven will copy its contents into the folder which will be on classpath during execution.

CodePudding user response:

It is because you do not start the Spring container yet. You have to call ClassPathXmlApplicationContext.refresh() to start it.

Another things is that you do not configure the location of the XML configuration file too.

So assuming the XML configuration file is called config.xml , please change to the following :

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext();
applicationContext.setConfigLocations("config.xml");
applicationContext.refresh();

Or simply :

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("config.xml");

which will call refresh() automatically under the cover.

  •  Tags:  
  • Related