Wednesday, June 13, 2012

Spring Framework- Bean Injections and Types


Java bean properties are private and each will have a pair of methods in form of set and get prefix.set methods are set the bean property so these called setter methods and get methods use to get the property(value) are called getter method. this concept is known as bean injections

Bean properties are injection many different ways as the requirements-.
1. Injecting simple values.
2.Injecting through constructor
3. Referencing other bean.
4.Injecting inner bean.
Lets Understand all these with an example-


directory Structure-
Class Point.java
package com.test;

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


Class Circle.java
package com.test;

public class Circle {

private Point p;

public Point getP() {
return p;
}

public void setP(Point p) {
this.p = p;
}

public void show()
{
System.out.println("Points are:\n X="+p.getX()+"\n Y="+p.getY());
}

}

Class Main.java
package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String args[]) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
ctx.getBean("circle", Circle.class).show();
}
}

Spring.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 name="point" class="com.test.Point">
<property name="x" value="10"></property>
<property name="y" value="20"></property>
</bean>
<bean id="circle" class="com.test.Circle">
<property name="p" ref="point"></property>
</bean>
</beans>


When Injecting simple values of beans than simple property tag is used and the property is injected with name and value pair.

                    <bean name="point" class="com.test.Point">
                    <property name="x" value="10"></property>
                    <property name="y" value="20"></property>
                    </bean>
To Inject through the Constructor need a constructor that hold the properties as arguments. to inject with the constructor need < constuctor-args> tag. Xml code will written as-
                    <bean name="point" class="com.test.Point">
                   <constructor-arg value="10"></constructor-arg>
                   <constructor-arg value="20"></constructor-arg>
                   </bean>
   this bean directly injected with the constructor which is overloaded into Point class-
                public Point(int x, int y) {
super();
this.x = x;
this.y = y;
         } 
 when a class contain object of other class as a property. Now to inject that property two things is use-
       first is referencing other beans as the value of that property -
                  <bean id="circle" class="com.test.Circle">
                  <property name="p" ref="point"></property>
                    </bean>
     here  ref is the reference of other point bean .but this is also accessible by others in that xml file.
so to make private Next is use inner bean injection-
                     <bean id="circle" class="com.test.Circle">
                    <property name="p" >
                     <bean class="com.test.Point">
                     <property name="x" value="10"></property>
                      <property name="y" value="20"></property>
                      </bean>
                      </property>
                       </bean>


in that code the bean property injected with other bean. but that bean is private and not accessible by others because inner bean not have any reference point to access.

Output is-
Points are:
X=10
Y=20


For Further Reading,
General, Java, spring

0 comments:

Post a Comment


 

Site Status

Man Behind Technical Today

Hello, I am Navin Bansal. I am a student of MCA in Rajsthan Institute of Engineering and Technology and owner of this blog. I share my view and ideas among people.