Wednesday, February 13, 2013

Spring Example - Spring Framework - Spring Tutorial - A Simple Example in Spring

Spring Example:

  
 The Spring Framework represents a dependency injection / inversion of control container framework for the Java platform. Furthermore Spring offers a lot of additional functions.Spring framework was initially.
                                   Written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.

Use Case:

Test Class             -      To create a main class in eclips.
Triangle Class       -      To create getter and setter in Triangle class.
Point Class            -      To create a point set the value.
Spring Class          -      To create a beans

Test Java class:

package Tutorial;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test
{
      public static void main(String[] args)
       {
              ApplicationContext aap=new ClassPathXmlApplicationContext("spring.xml");
              Triangle t=(Triangle)aap.getBean("tri");
              t.draw();
       }
}

Triangle class in Java:

package Tutorial;

import java.util.List;

public class Triangle
{
      
       Point point1;
       Point point2;
       Point point3;
      
public Point getPoint1() {
              return point1;
       }


       public void setPoint1(Point point1) {
              this.point1 = point1;
       }

       public Point getPoint2() {
              return point2;
       }

       public void setPoint2(Point point2) {
              this.point2 = point2;
       }

       public Point getPoint3() {
              return point3;
       }

       public void setPoint3(Point point3) {
              this.point3 = point3;
       }

public void draw()
{
      
      
       System.out.println("point1" + point1.getX()+"\tpoint2" + point1.getY());
}
}

Point Class in Java:

package Tutorial;

public class Point {
 int x;
 int y;
public int getX() {
       return x;
}
public void setX(int x) {
       this.x = x;
}
public int getY() {
       return y;
}
public void setY(int y) {
       this.y = y;
}  
}

Spring Class in Xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >

<beans>
<bean id="tri" class="Tutorial.Triangle" autowire="constructor" >
  
</bean>  

 <bean id="tri1" class="Tutorial.Point" >
 <property name="x" value="10"/>
 <property name="y" value="20"/>
 </bean>
 
</beans>     

0 comments:

Post a Comment