Skip to main content

Hello world application in spring using xml bean configuration.

1. Download the spring3.0 jars from following location.
 http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-3.0.6.RELEASE-with-docs.zip


2. Create a new Java project and include the following jars into your java build path. 


1.   org.springframework.aop-3.0.6.RELEASE.jar
2.   org.springframework.asm-3.0.6.RELEASE.jar
3.   org.springframework.aspects-3.0.6.RELEASE.jar
4.   org.springframework.beans-3.0.6.RELEASE.jar
5.   org.springframework.context-3.0.6.RELEASE.jar
6.   org.springframework.context.support-3.0.6.RELEASE.jar
7.   org.springframework.core-3.0.6.RELEASE.jar
8.   org.springframework.expression-3.0.6.RELEASE.jar
9.   org.springframework.instrument-3.0.6.RELEASE.jar
10. org.springframework.instrument.tomcat-3.0.6.RELEASE.jar
11. org.springframework.jdbc-3.0.6.RELEASE.jar
12. org.springframework.jms-3.0.6.RELEASE.jar
13. org.springframework.orm-3.0.6.RELEASE.jar
14. org.springframework.oxm-3.0.6.RELEASE.jar
15. org.springframework.spring-library-3.0.6.RELEASE.libd
16. org.springframework.test-3.0.6.RELEASE.jar
17. org.springframework.transaction-3.0.6.RELEASE.jar
18. org.springframework.web-3.0.6.RELEASE.jar
19. org.springframework.web.portlet-3.0.6.RELEASE.jar
20. org.springframework.web.servlet-3.0.6.RELEASE.jar
21. org.springframework.web.struts-3.0.6.RELEASE.jar


All the jars are present in spring-framework-3.0.6.RELEASE\dist folder


3. Create a new HelloWorldBean and include the following code.
package com.study.bean;

public class HelloWorldBean {
      private String message;
      public HelloWorldBean(){
           
      }
      public HelloWorldBean(String message){
            this.message = message;
      }
     
      public void setMessage(String message){
            this.message = message;
      }
      public String getMessgae(){
            return message;
      }
}

4. Create a new main class and include the following code.
                 

package com.bean.execution;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource
import com.study.bean.HelloWorldBean;
public class HelloWorldExecution {
public static void main(String args[]){
         BeanFactory factory = new XmlBeanFactory(new ClassPathResource("BeanConfig.xml"));
         HelloWorldBean helloWorldBean = (HelloWorldBean)factory.getBean("HelloWorld");
         System.out.println(helloWorldBean.getMessgae());
         helloWorldBean.setMessage("Modified message");
         System.out.println(helloWorldBean.getMessgae());
   }
}


5. Create a BeanConfig xml file inside the src folder and include the following code.

 <?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.xsd">

   <bean id="HelloWorld" class="com.study.bean.HelloWorldBean">
         <property name="message">
               <value>Hello World...!</value>
         </property>
   </bean>

</beans>
6. Execute the HelloWorldExecution main method

    I got the following errors.

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.<init>(DefaultSingletonBeanRegistry.java:83)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.<init>(FactoryBeanRegistrySupport.java:43)
    at org.springframework.beans.factory.support.AbstractBeanFactory.<init>(AbstractBeanFactory.java:174)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:154)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:165)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.<init>(DefaultListableBeanFactory.java:158)
    at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:72)
    at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
    at com.bean.execution.HelloWorldExecution.main(HelloWorldExecution.java:9)
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)
    ... 9 more

7. For resolving this issue, I download the Apache common logging jar from following location and I added the "commons-logging-1.1.1.jar" into my java build path.

      http://apache.mirrors.pair.com//commons/logging/binaries/commons-logging-1.1.1-bin.zip
      Now execute the HelloWorldMain class.

    The result of the application is
      Nov 30, 2011 12:33:12 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [BeanConfig.xml]
Hello World...!
Modified message



Comments

Popular posts from this blog

Getting key/value pair from JSON object and getting variable name and value from JavaScript object.

 Hi, I had faced one issue like this. I have an JSON object but I don't know any key name but I need to get the all the key and corresponding value from JSON object using client side JavaScript. Suddenly I wondered whether it's possible or not, after that I had done lot of workaround and finally got this solution. See the below example.    function getKeyValueFromJSON() {     var jsonObj =  {a:10,b:20,c:30,d:50} ;     for ( var key in jsonObj) {       alert( "Key: " + key + " value: " + jsonObj[key]);     }  }  In this example I have created the one json array as string, and converted this string into JSON object using eval() function. Using for-each loop I got all the key value from jsonObj, and finally using that key I got the corresponding value.  Finally I got the alert like this,    Key: a value:10    Key: b value:20    Key: c value:30    Key: d value:50  During this workaround I got one more idea, using this same way I got

Simple Login Application Using Spring MVC and Hibernate – Part 1

 I hope developers working in web application development might hear about MVC architecture. Almost all technologies provide support for MVC based web application development, but the success is based on many factors like reusable of the code, maintenance of the code, future adaption of the code, etc..,  The success of the Spring MVC is “ Open for extension and closed for modification ” principle. Using Spring MVC the developers can easily develop MVC based web application. We don’t need any steep learning curve at the same time we need to know the basics of spring framework and MVC architecture. The Spring MVC consists of following important components. 1. Dispatcher servlet 2. Controller 3. View Resolver 4. Model Spring MVC - Overview  The overall architecture of Spring MVC is shown here.  1. When “Dispatcher Servlet” gets any request from client, it finds the corresponding mapped controller for the request and just dispatches the request to the corresponding contro

Java Vs Groovy - Enum Demo

 Java 5 introduced enum that solve the problem with enumeration. Groovy also support that feature, as shown here.  Enum example using Java. enum ShirtSize { SMALL,MEDIUM,LARGE,EXTRALARGE } class ShirtImplementation { public void orderShirt(ShirtSize size) { switch(size) { case SMALL : System.out.println("The size of the shirt is small"); break; case MEDIUM : System.out.println("The size of the shirt is medium"); break; case LARGE : System.out.println("The size of the shirt is large"); break; case EXTRALARGE : System.out.println("The size of the shirt is extra large"); break; default : System.out.println("The shirt size is not available"); break; } } } public class EnumExample { public static void main(String args[]) { ShirtImplementation shirtImplementation = new ShirtImplementation(); shirtImplementation.orderShirt(ShirtSize.SMALL); shirtImplementation.o