0
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" class="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" class="Triangle"/>
</beans

Then also I am getting the same error

Guide me to do this spring programming perfectly.

Ajay9
  • 9
  • 2
  • Possibly helpful: https://stackoverflow.com/questions/4897212/refresh-reload-application-scope-managed-bean – sorifiend Jan 16 '22 at 08:12

2 Answers2

0

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.

Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • I have done that but still it is showing errors, And the XML file is in also correct classpath – Ajay9 Jan 16 '22 at 09:04
  • @Ajay9 please add the update to your question rather than update the question directly. Because it causes my answer does not match with your question after you update it directly , and cause some guy to downvote my answer. Thanks. – Ken Chan Jan 16 '22 at 10:10
  • Sure Ken, Thanks for letting me know Ken!! – Ajay9 Jan 18 '22 at 15:36
0

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.

Alexey Veleshko
  • 792
  • 1
  • 18