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