Released in the spring of 2003, Spring framework has been doing lot of heavy lifting of developing enterprise Java applications up to now. Compared with J2EE, Spring is very lightweight framework and consist of more elaborate features like Inversion of Control(IOC) container, Aspect Oriented Programming (AOP), Web MVC framework and so on.
In this tutorial, we are going to create an extremely simple traditional ‘hello world’ application using Spring framework and eclipse IDE. This would be very useful for Spring newbies.
Step 1: Download and install the Spring framework
Before we move further download the Spring framework from http://www.springsource.org/download. Spring comes in two flavors, with all dependencies and without dependencies. For beginners, I highly recommend you to choose the ‘with dependencies’ flavor because we don’t want to hunt for the dependencies later and it might take a whole day perhaps.
After downloading, extract it to a folder of your choice (for example, C:\ on Windows or /opt/ on UNIX)
If you examine the folder structure of Spring distribution, you would see following important folders
- dist – Contains spring framework.jar
- docs – Contains documents.
- lib – Contains all dependent libraries.
- src – Contains source code.
But don’t get confused with all these .jar files. To create our application, we only need the spring.jar file which resides in the dist folder and commons-logging.jar file in lib/jakarta-commons folder.
Now we have downloaded the Spring framework libraries. So lets setup a project using eclipse.
Step 2 : Setting up your project using eclipse
eclipse is a great IDE for enterprise Java application development. But you don’t have to limit only to eclipse, there are several IDEs out there such as NetBeans, IntelliJ IDEA and even Spring IDE ( a plugin for eclipse that make easier the Spring application development). So the selection is completely up to you. Here I choose d eclipse because I’m a fan of eclipse for a long time.
I’ll use the latest eclipse release eclipse Galileo for Java EE development. You can download it by visiting http://www.eclipse.org/downloads/
So lets begin coding our application
Fire up eclipse and go to File > New Java Project. This will bring you the new project creation dialog. Enter HelloSpring as the project name and be sure to set Project Layout as Create separate folders for sources and class files. Leave other options as it is and click on Next.
Then eclipse will show you another dialog and ask you to configure your Java Build Path. Click on the Libraries tab and select Add External Jars. This is place where we can add Spring framework libraries to our project. So browse for the Spring extraction folder and select dist/spring.jar and lib/jakarta-commons/commons-logging.jar.
This would like the following.
Then click on Finish.
Step 3: Create a simple Spring bean.
Now we’ve setup the project and configured it to get Spring support. All we have to do is start coding the application.
As the first step, I’m gonna create a simple Java class called “HelloSpring”.
public class HelloSpring { private String message; public void setMessage(String message) { this.message = message; } public String getMessage() { return message; } public void sayHello() { System.out.println(message); } }
This is a kind of Spring bean which is very similar to simple POJO class. It has a property called ‘message’ and we are going to print a message to the screen using this class. Create the above class using eclipse and place it under com.dunithd.hellospring package.
Step 4: Create beans.xml file
We need to tell Spring about our HelloSpring bean. For this purpose, Spring uses a bean descriptor file which is a XML file and holds information about all Spring beans in your application.
Create the following XML file in your classpath and name it as beans.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
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="helloSpring" class="com.dunithd.hellospring.HelloSpring">
<property name="message" value="Hello Spring!" /> </bean> </beans>
Step 5 : Create the main program
I’m gonna create a simple Java program which prints ‘Hello Spring!” to the screen. So I should have main method which invokes the HelloSpring bean.
Create the following class and place it under the same package.
package com.dunithd.hellospring; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //load the beans.xml file from the project's classpath ApplicationContext context = new ClassPathXmlApplicationContext( "beans.xml"); BeanFactory factory = (BeanFactory) context; //build the factory //obtain the bean from factory HelloSpring helloSpring = (HelloSpring) factory.getBean("helloSpring"); //Most of the heavy lifting is done by Spring, now use the helloSpring object as your wish helloSpring.sayHello(); } }
By looking at the main program we can observe that Main class does not directly instantiate the HelloSpring bean, instead it delegates the Creation Responsibility to the Spring container. So in this
case, Spring acts as a kind of Factory object. This is the most important aspect of Spring, which is Ability to Create Objects and this approach leads us to create loosely coupled software components.
Step 6 : Add logging configuration file to the project
The Spring framework uses Apache Commons Logging (about which information is available at http://jakarta.apache.org/ commons/logging/) to log container and application information . We’ve add the commons-logging.jar file to our project because of this reason.
So create a file called log4j.properties and place it under root of the classpath with following content.
log4j.rootLogger=FATAL, first log4j.appender.first=org.apache.log4j.ConsoleAppender log4j.appender.first.layout=org.apache.log4j.PatternLayout log4j.appender.first.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Now we have reached to the end of our application development. If you followed the above steps correctly, your eclipse project would looks like follows.
Now right click on the Main.java file and select Run As > Java Application from the context menu.
Then you’ll see an output like this.
You can change the output message by changing the message property in beans.xml.
What we have done so far?
Using beans.xml, we’ve told Spring that what types of beans should be instantiated and what properties should be injected to that beans. As you saw, Spring container reads the message property from beans.xml and injected it into the HelloSpring bean.
Beside these things, Main program is not aware of creating beans and injecting dependencies. So the whole application is well decoupled.
Thats the power of Spring!
This is very simple application of Spring and this doesn’t reflects the full Inversion of Control capability of that. But if you are a Spring newbie, this would be a good starting point.
Download the source code for the HelloSpring application here
Now right click on the Main.java file and select Run As > Java Application from the context menu.Then you’ll see an output like this.You can change the output message by changing the message property in beans.xml.What we have done so far?
Using beans.xml, we’ve told Spring that what types of beans should be instantiated and what properties should be injected to that beans. As you saw, Spring container reads the message property from beans.xml and injected it into the HelloSpring bean.
Beside these things, Main program is not aware of creating beans and injecting dependencies. So the whole application is well decoupled.
Thats the power of Spring!
This is very simple application of Spring and this doesn’t reflects the full Inversion of Control capability of that. But if you are a Spring newbie, this would be a good starting point.
Nice write up men! and welcome to the blogging world.
Yeah!Anyone can really learn something from this…. Congradulations….!
Hi , I m new to Spring and tried my first application as you instructed.Tell me what is the meaning of
“Create the following XML file in your classpath and name it as beans.xml”.
Bcoz i m getting error
“Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist
java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:127)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:144)”
Hi Abhilasha,
Make sure you’ve put beans.xml file in your classpath.
Please refer the figure “Package explorer view in eclipse” to get a better idea about the project layout. Then create the beans.xml file and place it with other .java files that you use to write your application.
Hi Dunith, I just followed steps told by you in this article…bt m gettin followin error while runing Main.java
Feb 20, 2011 6:39:09 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1bf52a5: startup date [Sun Feb 20 18:39:08 IST 2011]; root of context hierarchy
Feb 20, 2011 6:39:09 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [beans.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(AbstractXmlApplicationContext.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:509)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:427)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at home.saharsh.Main.main(Main.java:11)
Caused by: java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
… 13 more
So, can u please help me out with this. My package hierarchy is same as you show above and beans.xml is along with other .java files.
Thanks in advace…..
Hi Amit,
Can you exactly tell me where did you put the beans.xml file? I suggest you to place it inside src folder.
Hi Dunith
thanks for prompt reply……
It is already in src folder…inside which i hv a package name com.amit and in that package i hv both the java file n xml file n lo4j file.
bt still not working….i don know why
The same here i got this error :
Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘helloSpring’ defined in class path resource [beans.xml]: Instantiation of bean failed; nested exception is java.lang.IllegalStateException: No bean class specified on bean definition
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:900)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455)
Hi Majid,
Can you post the contents of your beans.xml file here? It would be helpful to sort out this issue.
I am getting this error:
Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.springframework.context.support.AbstractApplicationContext.(AbstractApplicationContext.java:161)
at org.springframework.context.support.AbstractRefreshableApplicationContext.(AbstractRefreshableApplicationContext.java:90)
at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.(AbstractRefreshableConfigApplicationContext.java:59)
at org.springframework.context.support.AbstractXmlApplicationContext.(AbstractXmlApplicationContext.java:61)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:136)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.dunithd.hellospring.Main.main(Main.java:11)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
… 7 more
Pls.help
Hi Kunal,
This happened because of the missing common-logging jar. You can download it from here. Once you’ve downloaded it, put that into to your classpath and try to rebuild the code.
i think u are specified logFactory file inside a package so please remove from the package specify in the src folder in your project
thanks Dunith..now I rectified the error..but I am now getting this error:
ay 12, 2011 3:55:49 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@32fb4f: startup date [Thu May 12 15:55:49 GMT+05:30 2011]; root of context hierarchy
May 12, 2011 3:55:49 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
May 12, 2011 3:55:50 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15b0afd: defining beans [HelloSpring]; root of factory hierarchy
May 12, 2011 3:55:50 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15b0afd: defining beans [HelloSpring]; root of factory hierarchy
Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘HelloSpring’ defined in class path resource [beans.xml]: Instantiation of bean failed; nested exception is java.lang.IllegalStateException: No bean class specified on bean definition
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:900)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93)
at com.dunithd.hellospring.Main.main(Main.java:11)
Caused by: java.lang.IllegalStateException: No bean class specified on bean definition
at org.springframework.beans.factory.support.AbstractBeanDefinition.getBeanClass(AbstractBeanDefinition.java:372)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:52)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
… 13 more
and beans.xml which is in src folder is this:
///////
/////
I am getting error with IllegalStateExceptio.
This is my Main.java (part):
ApplicationContext context =
new ClassPathXmlApplicationContext(“beans.xml”);
BeanFactory factory = (BeanFactory) context; //build the factory
//obtain the bean from factory
HelloSpring hs = (HelloSpring) factory.getBean(“helloSpring”);
//Most of the heavy lifting is done by Spring, now use the helloSpring object as your wish
hs.sayHello();
This is my beans.xml:
I have the HelloSpring.java, Main.java and beans.xml in same directory and using this as Classpath frm Eclipse GUI Run Configurations. Still getting this error:
Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘helloSpring’ defined in class path resource [beans.xml]: Instantiation of bean failed; nested exception is java.lang.IllegalStateException: No bean class specified on bean definition
Caused by: java.lang.IllegalStateException: No bean class specified on bean definition
at org.springframework.beans.factory.support.AbstractBeanDefinition.getBeanClass(AbstractBeanDefinition.java:287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:714)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:386)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:249)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:155)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:291)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:122)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:66)
at com.dunithd.hellospring.Main.main(Main.java:14)
Can someone please help me? I am new in Spring.
Thanks very much for this example. This is small, easy to understand and test – though I am faltering!
Gautam
Hi Gautam,
Could you please post your beans.xml content here?
I guess this is due to the mismatching of the bean name definition in the beans.xml file and your calling code.
-Dunith
Dunith – I am copy-pasting the beansxml, but they are vanishing!. Sorry to make so many posts. Wish I could delete them.
Thanks
Gautam
You are getting this error as your classpathentry might be restricted. This post should be helpful
http://dev-faqs.blogspot.in/2012/12/configuring-build-path-in-eclipse-for.html
Thanks Dunith. Here it is:
Regards
Gautam
Dunith
I have placed ‘//’ in front of all lines. Hope this works, else you are free to delete this post. Thanks.
//
//
//
//
//
//
Regards
Gautam
I have removed all tags for the last essential 3 lines:
bean id=”helloSpring”
property name=”message” value=”Hello Spring!”
bean
Hi Gautam,
When defining a bean in the beans.xml, id and class attributes MUST be set. In my post I forgot to mention that.
In order to solve your problem, open up your beans.xml file and add the ‘class’ attribute to the helloSpring bean. I’ve updated the beans.xml for that.
Hope it’ll fix your problem.! 🙂
-Dunith
Thanks very much Dunith. Sorry to bother you with such dumb question/issue. It’s working.
Regards
Gautam
Hi Dunith,
I m getting this exception on processing above code & i already placed common-loggings.jar at class path, what should i do, please help me, i m new in spring
Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.springframework.context.support.AbstractApplicationContext.(AbstractApplicationContext.java:161)
at org.springframework.context.support.AbstractRefreshableApplicationContext.(AbstractRefreshableApplicationContext.java:90)
at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.(AbstractRefreshableConfigApplicationContext.java:59)
at org.springframework.context.support.AbstractXmlApplicationContext.(AbstractXmlApplicationContext.java:61)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:136)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.dnit.helloworld.Main.main(Main.java:10)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
… 7 more
Thanks & Regards,
Priyanka Dwivedi
Yeah!Anyone can really learn something from this…. thanks….!
This was a great tutorial. To all the people getting errors, move the beans.xml out of the package so it is just in the src folder. That was my problem at least.
Ok Dunith Now I have a question. I am trying to apply this set up to a dynamic web project in eclipse. I am trying to hybridize your tutorial on reading XML and the tutorial here:
http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/
I have re-created this tutorial as well and all I want is that the HelloWorldController’s main method calls this Main method so that the message I get back is from the XML file rather than a string I create.
Basically I want to apply this tutorial to a dynamic web project so that the final message writes to an HTML webPage, not just a system out.
Please and Thanks,
Alex
Hi Alex,
Thanks for the very descriptive and supportive comment on the post! 🙂
I’d like to advise you regarding the attempt of applying this tutorial on a Spring Web MVC project.
First, the above tutorial is targeted at standalone Java applications that are used to be console or desktop applications which runs independently. So they could have a main method to bootstrap the execution.
But Eclipse dynamic project is a web application that resides on a web container (like tomcat). They are cant be executed by themselves so that web applications(servlets) don’t expose a main method. Instead you can write URL handlers that would respond to various URL mappings.
I’m not quite sure about your requirement. If you want to write a message that is in a xml file into a web page, best option isto follow some Spring MVC tutorials. Using a properties file you could achieve that very easily.
regards,
Dunith
sreenivas was saying
I am getting an error. that is
Dec 02, 2012 12:35:06 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@8121e5a: startup date [Sun Dec 02 12:35:06 PST 2012]; root of context hierarchy
Dec 02, 2012 12:35:06 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [beans.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:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:126)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:92)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at info.inetsolv.Main.main(Main.java:13)
Caused by: java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
… 13 more
how to solve the this error tell me any body knows this problem
Hi…dunithd
i am getting error like
Apr 19, 2013 12:17:57 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: Line 6 in XML document from class path resource [beans.xml] is invalid; nested exception is org.xml.sax.SAXParseException: Document root element “beans”, must match DOCTYPE root “null”.
org.xml.sax.SAXParseException: Document root element “beans”, must match DOCTYPE root “null”.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.rootElementSpecified(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:126)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:125)
at org.springframework.context.support.AbstractXmlApplicationContext.refreshBeanFactory(AbstractXmlApplicationContext.java:65)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:226)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:58)
at com.HelloSpring.Main.main(Main.java:12)
and my Beans.xml is:
and my hierarchy is also same as ur project explore but dont know what is going on…pls give a solution thnx…:)
Hi Milan,
Looks like your beans.xml file is malformed. It might conatin some syntatic errors.
Given below is a link to a sample beans.xml file so that you could start off with it.
https://dl.dropboxusercontent.com/u/18954958/beans.xml
this is my Beans.xml file
I am not allowed to post my beans.xml file in comment!!!!