Sunday, 3 September 2017

How Spring container and Bean factory works ?


Container
Servlet container of Tomcat creates servlet objects which are required to run the application.
You configure all the servlets in web.xml and provide classes.
Tomcat reads XML and creates servlet objects.

Spring container
Similarly, Spring provides container of beans. This container handles :
  • Instantiation of objects
  • Life cycle of objects
  • Destruction of objects

When you create object using new keyword, Spring doesn't know it and will not manage it.

Bean Factory
Spring reads configuration / annotations and uses Factory pattern to create objects.
XML --> Bean factory --> objects

Saturday, 2 September 2017

ref vs. idref


ref attribute vs. idref tag

ref attribute can point to a Bean ID, Name or Alias, but idref tag only point to Bean ID.
IDRef makes sure there should be a bean defined with the specified bean ID, otherwise throw an exception.

<bean id="triangle" class="com.shaan.exmaple.Triangle">
    <property name="point1" ref="pointOne" />
</bean> 
Looks for "pointOne" as a bean ID, bean name or alias.


<bean id="triangle" class="com.shaan.exmaple.Triangle">
   <property name="point1">
      <idref="pointOne" />
   </property>
</bean>
Looks for "pointOne" in the bean IDs only.

What is the use of Bean aliases and Bean name ?


Bean alias is another ID given to a bean and can be used as another bean ID for same object creation.
Aliases can be defined using 2 ways : Using alias tag and using bean name attribute

Example 1. Using alias tag
XML
<bean id="triangle" ..>
</bean>

<alias name="triangle" alias="triangle-alias" />


USAGE
BeanFactory factory = new XmlBeanFactory(new   
FileSystemResource("spring.xml"));

Triangle t = (Triangle) factory.getBean("triangle-alias");
t.draw();


Example 2. Using bean name attribute
XML
<bean id="triangle" .. name="triangle-alias">
</bean>



How to create a Spring applicaiton ?




CREATING SPRING EXAMPLE


PreRequisites
  • Download Spring Jars and Apache Logging Jars
  • In eclipse, New > Java project
  • Properties > Java build path > Add Lib / Jars
    • Choose {downloaded package}/dist/*.jar
    • Choose Commons-logging-*.jar
Code
public class DrawingApp {
  p.s.v. main(String[] args) {
    // Triangle t = new Triangle();
    BeanFactory factory = new XmlBeanFactory(new   

    FileSystemResource("spring.xml"));
    Triangle t = (Triangle) factory.getBean("triange");
    t.draw();
  }
}
public class Triangle {
  public void draw() {
    s.o.p("Draw the Triangle");
  }
}

 spring.xml - Use code from a sample ApplicationContext.xml
<beans>
   <bean id="triangle" class="com.shaan.exmaple.Triangle" />
</beans>



How to reference beans or use Object injection using Spring configuration ?


USE ref attribute of property tag. 
<property name="xyz" ref="bean2">

EXAMPLE
Class definition
public class Point {
   private int x;
   private int y; 
   // getters n setters
}  
public class Triangle {
   private Point point1;
   private Point point2;
   private Point point3;
   // getters n setters
   public void draw(){
     s.o.p.("Point1:"+ getPoint1().getX() +","+ getPoint1().getY());
            s.o.p.("Point2:"+ getPoint2().getX() +","+ getPoint2().getY());
            s.o.p.("Point3:"+ getPoint3().getX() +","+ getPoint3().getY());
   } }

XML
<beans> 
   <bean id="pointOne" class="com.shaan.exmaple.Point">
      <property name="x" value="0" />
      <property name="y" value="0" />
   </bean>
   <bean id="pointTwo" class="com.shaan.exmaple.Point">
      <property name="x" value="-20" />
      <property name="y" value="0" />
   </bean>
   <bean id="pointThree" class="com.shaan.exmaple.Point">
      <property name="x" value="20" />
      <property name="y" value="0" />
   </bean>
   <bean id="triangle" class="com.shaan.exmaple.Triangle">
      <property name="point1" ref="pointOne" />
      <property name="point2" ref="pointTwo" />
      <property name="point3" ref="pointThree" />
   </bean>
</beans>


USAGE
BeanFactory factory = new XmlBeanFactory(new   
FileSystemResource("spring.xml"));

Triangle t = (Triangle) factory.getBean("triange");
t.draw();

Wednesday, 30 August 2017

What is IoC ?


If Object A depends on object B, IoC is to remove its dependency.

Example 1. Need to hardcode 'Circle' or 'Triange' for object creation.
Circle c = new Circle()
c.draw();
Triange t = new Triange();
t.draw();

 
Example 2. POLYMORPHISM, but still hardocded to use Circle or Triange
Aplication class knows which one to use.

Shape c = new Circle()
c.draw();
Shape t = new Triange();
t.draw();


Example 3. Create a method to pass a generic type (parent class/interface)
TRUE POLYMORPHISM, DYNAMIC, LOOSELY COUPLEDBut still somewhere in the class the object should to be created and passed to method1.

method1(Shape shape) {
    shape.draw();
}

 
Example 4. Create a separate class in which object is not created.
CLASS INDEPENDENT OF THE USAGE

class Drawing {
   private Shape shape;
   public setShape(Shape shape){
       this.shape = shape;
   }

   public drawShape(){
       this.shape.draw();
   }

}
 

// Dependency injected in some other class
Triange triange = new Triange();
drawing.setShape(triange);
drawing.drawShape();

How to create a Spring application and use BeanFactory ?


PreRequisites

  • Download Spring Jars and Apache Logging Jars
  • In eclipse, New > Java project
  • Properties > Java build path > Add Lib / Jars
    • Choose {downloaded package}/dist/*.jar
    • Choose Commons-logging-*.jar
Code
public class DrawingApp {
  p.s.v. main(String[] args) {
    // Triangle t = new Triangle();
    BeanFactory factory = new XmlBeanFactory(new   

    FileSystemResource("spring.xml"));
    Triangle t = (Triangle) factory.getBean("triange");
    t.draw();
  }
}
public class Triangle {
  public void draw() {
    s.o.p("Draw the Triangle");
  }
}

spring.xml - Use code from a sample ApplicationContext.xml
<beans>
   <bean id="triangle" class="com.shaan.exmaple.Triangle" />
</beans>