First Program in spring framework using eclipse IDE


Spring is powerful framework also called mother of all Framework



First Program in Spring

1.  Calulator.java

package com.login;

public class calculator implements operation{

    public calculator()
    {
        System.out.println("Inside the calculator");
    }
public int sum()
{
    return 4;
   
}
public  int substraction()
{
    return 6;
}
}




2. operation.java

package com.login;

public interface operation {
    public int sum();
    public int substraction();
   

}



3. TestCal.java

package com.login;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

public class TestCal {
    public static void main(String[] args)
    {
        try{

BeanFactory factory = new XmlBeanFactory(new ClassPathResource("aplication.xml"));   

calculator cl=(calculator)factory.getBean("bean1");
int a=0;
a=cl.sum();
System.out.println("The value of sum is "+a);
        }
        catch(Exception e)
        {
            System.out.println("The exception is"+e.getMessage());
        }
       
    }

}


Outpt:

After the run this program Output will
  Inside the calculator
  The value of sum is 4

Here we bean class Calcultor is not directly instantiated , it has done through XMLBeanFactory i.e. configure into
application.xml file, so spring framework says that programmer to concentrate only on bussiness loging not on creation of object
i.e. Spring IOC take responsibility of creation of object

application.xml

<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-2.0.xsd">

  <bean id="bean1" class="com.login.calculator"/>
  <bean id="audi" class="com.aspec.Audience"/>
</beans> 

Import following highlighted jar file in lib directory



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.