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

Ext JS 4 – Creating web page using Ext.container.Viewport class and region propery

The Ext.container.Viewport is used for creating general web page and region property is used for splitting web page into different parts. Ext.container.Viewport is a specialized container represents the viewable area of the application (the browser area). It render itself to the document body, there is no need for providing renderTo property and it automatically sizes itself to size of the browser viewport. The viewport also re-size the child elements based on view area(based on browser width and height). The default layout of the viewport is border layout and we can customize this property according to our requirements. The viewport does not provide any scrolling. If necessary, the child elements(generally panels) within viewport needs to provide a scroll feature by using autoScroll property. See the below example for better understanding. This is Home.js file placed into app/view folder. Ext.define('MyApp.view.Home', { extend : 'Ext.container.Viewport&#

Getting servlet init parameter that is defined in web.xml

1. Create a new dynamic web project using eclipse. 2. Create a one new servlet and include the following code. package com.controller ; import java.io.IOException; import javax.servlet .ServletConfig; import javax.servlet .ServletException; import javax.servlet .annotation.WebServlet; import javax.servlet .http.HttpServlet; import javax.servlet .http.HttpServletRequest; import javax.servlet .http.HttpServletResponse; @ WebServlet ( "/LoginController" ) public class LoginController extends HttpServlet {                 private static final long serialVersionUID = 1L;                 public LoginController() {         super ();     }     protected void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {                                 ServletConfig config = getServletConfig ();                                 System.out.println( "Init parameter user nam

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