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......

0 comments: