Showing posts with label spring framework. Show all posts
Showing posts with label spring framework. Show all posts

First Hello World example in Spring framework by using DI.

Hi Friend when you going start learn Spring Framework you will start to write first Hello World Program
Below is step by step to write first program

1. Write the applicationContext.xml (bean container)

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-2.5.xsd
     http://www.springframework.org/schema/util
     http://www.springframework.org/schema/util/spring-util-2.5.xsd"
     xmlns:context="http://www.springframework.org/schema/context">
<context:annotation-config/>
<context:component-scan base-package="com.dao"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="cal" class="com.test.calculation" init-method="init">
<property name="name" value="kumud"></property>
</bean>
</beans>

2. Write bean class for calculation

package com.test;
import org.springframework.beans.factory.BeanNameAware;
public class calculation  implements BeanNameAware{
    public String name = null;
    public String getName() {
        return this.name;
    }
    public void init() {
        System.out.println("Inside the init methos");
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setBeanName(String arg0) {
        System.out.println("setting the bean name ********************");
    }
}

3. write TestClass

package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestClass {
    public static void main(String [] args)
    {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        calculation calc=(calculation)ctx.getBean("cal");
        System.out.println("The name of is"+calc.getName());
        }

}