Spring first HelloWorld example

Here I will try to walk through the Spring3.0 first example, which will help you in understanding the spring workflow. and classes and xml required to complete one request process, here we will work on sample java application and not a web application.

Here we are creating following components to run Spring java application, but before starting please read through the configuration required for Spring3, this will help you setup spring in eclipse.

Lets create a simple class with sayHello method in it, and put some instructions so that you can see something happening in console here I have put SOP which will print a line Hello from Spring 3.0.


package com.tekhnologia.spring3.example1;
public class Spring3HelloWorld {
public void sayHello(){
System.out.println("Hello from Spring 3.0");

Spring use IOC(Inversion of Control) to create object of any class, so we have to put entry into xml file called SpringHelloWorld.xml, not required to be same as above class.
Here we are creating bean tag and putting id as a name which will be used and known to class which will be going to use the object of the above bean class. and class attribute will have full path of class going to be initialized. 

As IOC is stated for dependeancy injection which means you are not creating dependency but container is doing on behalf of you, here is the example of doing that, here we container is creating runtime  dependency by loading below xml file where ever class object is required.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="Spring3HelloWorldBean"
class="com.tekhnologia.spring3.example1.Spring3HelloWorld" /></beans>

After creating xml in which dependency is declared, we are going to use above xml to create object of bean class at step 1. We will use XmlBeanFactory to create instance for resource which will take ClassPathResource as parameter which will SpringHelloWorld.xml and now you can use XmlBeanFactory as below to create object of Bean class.

package com.tekhnologia.spring3.example1;
import java.util.Map;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.Assert;
public class Spring3HelloWorldTest {
public static void main(String[] args) {
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource ("SpringHelloWorld.xml"));
Spring3HelloWorld myBean = (Spring3HelloWorld) beanFactory .getBean("Spring3HelloWorldBean");

Click here to learn spring with example.

Must Read Articles:
We frequently update my articles to best of my knowledge, please touch base with us to get maximum.
If you like reading Please share with others or provide feedback to make it better. 

Bhanu Pratap