Thursday 13 May 2010

Example program for Injecting Objects

Example program: steps: (Error in this program)
1. import the jar files (set the class path)
2. xml file
3. java files






//DrawingApp .java
package org.rajendra.spring;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;

public class DrawingApp 
{
public static void main(String args[])
{
//Triangle triangle=new Triangle();
//using bean Factory
//BeanFactory factory=new XmlBeanFactory(new FileSystemResource("spring.xml"));
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle=(Triangle)context.getBean("triangle");
triangle.draw();
}

}

/*Point.java*/

package org.rajendra.spring;

public class Point
{
private int x;
private 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;
}


}
//Triangle.java

package org.rajendra.spring;

public class Triangle 
{
private Point pointA;
private Point pointB;
private Point pointC;
//use spring dependency injection to pre populate
 public Point getPointA() {
return pointA;
}

public void setPointA(Point pointA) {
this.pointA = pointA;
}

public Point getPointB() {
return pointB;
}

public void setPointB(Point pointB) {
this.pointB = pointB;
}

public Point getPointC() {
return pointC;
}

public void setPointC(Point pointC) {
this.pointC = pointC;
}

public void draw()
 {
System.out.println("Point A=("+getPointA().getX()+","+getPointA().getY()+")");
System.out.println("Point B=("+getPointB().getX()+","+getPointB().getY()+")" );
System.out.println("Point c=("+getPointC().getX()+","+getPointC().getY()+")");
 
 }
}

//spring.xml file

<?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="triangle" class="org.rajendra.spring.Triangle">
 <property name="pointA" ref="zeroPoint"/>
 <property name="pointB" ref="point2"/>
 <property name="pointC" ref="point3"/>
 </bean>
         

<bean id="zeroPoint" class="org.rajendra.Point">
 <property name="x" value="0"/>
 <property name="y" value="0"/>
</bean>
<bean id="point2" class="org.rajendra.Point">
 <property name="x" value="-20"/>
 <property name="y" value="0"/>
</bean>
<bean id="point3" class="org.rajendra.Point">
 <property name="x" value="20"/>
 <property name="y" value="0"/>
</bean>
</beans>




Friday 7 May 2010

Introduction to Spring

Information about the Spring Framework

Type:            Application Framework
Version:        3.x/4.x (Compatible with jdk 1.6+)
Vendor:         Interface 21
Creator:         Mr. Rod Jhonson

  • It is a open source.

To Download: download as zip file from
                         www.springframework.org
                         www.io.spring.org.
                         (spring-framework-3.0.5. Release-with-docs.zip)

To install software: Extract zip file


<sp_home>\dist: which gives spring libraries (jar files0
<sp_home\docs: which gives api docs, reference doc
<sp_home\project:which gives sample apps,projects
<sp_home>\src:  which gives source code.


Example program for spring
ApplicationContext
Spring            What is Spring

How Spring fits into the Enterprise world


Spring Modules

next topic       Spring Core

Spring Core


Example program for springs

Example program for dependency injection: with using springs and without using spring

/*  DrawingApp.java */
package org.rajendra.spring;

public class DrawingApp 
{
public static void main(String args[])
{
Triangle triangle=new Triangle();

triangle.draw();
}

}
javac DrawingApp.java

package org.rajendra.spring;

public class Triangle 
{
 public void draw()
 {
System.out.println("Triangle drawn");
 
 }
}

javac Triangle .java
java DrawingApp

//with using spring:
  • download the spring jar files
  • set the class path of spring jar files
like below


package org.rajendra.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class DrawingApp 
{
public static void main(String args[])
{
//Triangle triangle=new Triangle();
//using bean Factory
BeanFactory factory=new XmlBeanFactory(new FileSystemResource("spring.xml"));
Triangle triangle=(Triangle)factory.getBean("triangle");
triangle.draw();
}

}




package org.rajendra.spring;

public class Triangle
{
 public void draw()
 {
System.out.println("Triangle drawn");

 }
}


  • now create a xml file for beans

<?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="triangle" class="org.rajendra.spring.Triangle"/>
</beans>

  • now execute the program