Sunday, February 2, 2014

Step by Step to develop Spring4.0 Application

Step1:  Create Java Project

The first step is to create a simple Java Project using Eclipse IDE. Follow the option File -> New -> Project and finally select Java Project wizard from the wizard list. Now name your project as HelloWorld.

Step 2 - Add Required Libraries:

Add Spring Framework and common logging API libraries in HelloWorld project. To do this, right click on your project name HelloWorld and then follow the following option available in context menu: Build Path -> Configure Build Path to display the Java Build Path.

Now use Add External JARs button available under Libraries tab to add the following core JARs from Spring Framework and Common Logging installation directories:
Windows:  
SPRING4_HOME=E:\ranga\MyFiles\Spring\Software\spring-framework-4.0.0.RELEASE
Linux: 
SPRING4_HOME=/home/ranga/MyFiles/Spring/Software/spring-framework-4.0.0.RELEASE
%SPRING4_HOME%/libs/spring-core-4.0.0.RELEASE.jar
%SPRING4_HOME%/libs/spring-beans-4.0.0.RELEASE.jar

commons-logging-1.1.3.jar

Step 3 - Create HelloMessage Interface (HelloMessage.java) :
package com.ranga;
 
public interface HelloMessage {
public void sayHello();
}

Step 4 - Create HelloMessageImpl Class (HelloMessageImpl.java) :

package com.ranga;

public class HelloMessageImpl implements HelloMessage{
@Override
public void sayHello() {
System.out.println("Hello Ranga!");
}
}

Step 5 - Create Bean Configuration file (beans.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<bean id="helloMessage" class="com.ranga.HelloMessageImpl"></bean>

</beans>

Step 6 - Create Client Application (Application.java):

package com.ranga;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Application {
public static void main(String[] args) {
Resource resource = new ClassPathResource("beans.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
HelloMessage helloMessage = (HelloMessage) beanFactory.getBean("helloMessage");
helloMessage.sayHello();
}
}

Step 7 - Run the Application (Application.java):
Feb 2, 2014 11:31:26 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Hello Ranga!

Next Program We can see by using Annotations and Java Configuration
 .............
Coming soon......

Related Posts:

  • CRUD Operations Using HibernateBelow example explains how to perform Create, Read, Update and Delete (CRUD) operations using Hibernate.Tools: Eclipse,MySQLHiberante 4.3.6Project Structure:Step1: Creating the POJO classEmployee.javapackage com.var… Read More
  • Creating our Own Hibernate Template classLet us see how to develop our own Hibernate Template class.HibernateTemplate.javaimport java.io.Serializable;import java.util.List;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Sessio… Read More
  • Hibernate Object Identity and EqualityThere are several mismatches between the Object oriented system and Relation system. In that one of the important mismatch is Object identity and equality.There are three ways to tackle identity: two ways in java world, and o… Read More
  • Creating Custom Generator class in HibernateIn this post, we are going to learn how to create custom generator class in hibernate.Hibernate supports many built in generator classes like assigned, sequence, increment, identity, native etc. But some requirements the… Read More
  • Find duplicate elements in ArrayListThere are four ways is there to find duplicate element in arraylist.Using ArrayList contains method   Using HashSetUsing HashMapWithout using abovepackage com.varasofttech;import java.util.*;/** * @author Ranga Redd… Read More

0 comments: