I have worked with many Java frameworks, including Struts before. I recently posted about Struts. Here is a simple “hello world” using Spring:

http://www.javatpoint.com/steps-to-create-spring-application

The instruction is to use Java on command line and not use any IDE. I tried this, it works. Except in step 5, if you are running from command line, you need to include the jars used in the classpath. Otherwise you get an error. (If you are using an IDE, Eclipse for e.g., you will have to configure project classpath correctly. I will write something on that later).

If you just do what the author suggests,

javac -d . Student.java
javac -d Test.java

You will get the following error:

Test.java:3: error: package org.springframework.beans.factory does not exist
import org.springframework.beans.factory.BeanFactory;
^
Test.java:4: error: package org.springframework.beans.factory.xml does not exist
import org.springframework.beans.factory.xml.XmlBeanFactory;
^
Test.java:11: error: cannot find symbol
BeanFactory factory=new XmlBeanFactory(resource);
^
symbol:   class BeanFactory
location: class Test
Test.java:11: error: cannot find symbol
BeanFactory factory=new XmlBeanFactory(resource);
^
symbol:   class XmlBeanFactory
location: class Test
4 errors

To run it properly please try,

C:\ranga\java\fsp>javac  -classpath .\lib\org.springframework.beans-3.0.1.RELEASE-A.jar;.\lib\org.springframework.core-3.0.1.RELEASE-A.jar;.\lib\com.springsource.org.apache.commons.logging-1.1.1.jar;.; -d . Test.java

C:\ranga\java\fsp>

Run it using Java command:

C:\ranga\java\fsp>java  -classpath .\lib\org.springframework.beans-3.0.1.RELEASE-A.jar;.\lib\org.springframework.core-3.0.1.RELEASE-A.jar;.\lib\com.springsource.org.apache.commons.logging-1.1.1.jar;.; com.javatpoint.Test

org.springframework.beans.factory.xml.XmlBeanDefinitionReaderloadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Hello: Ranga Kalyan

That is “hello world” using Spring library. As the author notes, you create simple POJO classes and they become Spring beans at runtime. To do this, you just need to set up the config file (applicationContext.xml). See the Original post for more details.

Advertisements