Spring automatically define the how to wire beans together by setting autowire property on each bean. it remove lot of Xml code writing overheads.To autowire the properties with name set the autowire with "byType".it find the bean in container whose type of class is matched if not matched or more than one matched is found than an exception is thrown by -orq.springframework.beans.factory.UnsatisfiedDependencyEception.
Here we understand this feature with example-
Directory Structure-
class Draw.java
<?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="point" class="com.test.Point">
<property name="x" value="10"></property>
<property name="y" value="20"></property>
</bean>
<bean id="draw" class="com.test.Draw">
</bean>
<bean id="triangle" class="com.test.Triangle" autowire="byType">
</bean>
</beans>
output is-
Here we understand this feature with example-
Directory Structure-
class Draw.java
package com.test;
public class Draw {
public void draw()
{
System.out.println("Triangle Drawn");
}
}
class Point.java
public class Draw {
public void draw()
{
System.out.println("Triangle Drawn");
}
}
class Point.java
package com.test;
public class Point {
private int x,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;
}
}
class Triangle.javapublic class Point {
private int x,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;
}
}
package com.test;
public class Triangle {
private Point point1;
private Draw dr;
public Point getPoint1() {
return point1;
}
public void setPoint1(Point point1) {
this.point1 = point1;
}
public void show()
{
System.out.println("Ponit1 x="+point1.getX()+"y="+point1.getY());
dr.draw();
}
public Draw getDr() {
return dr;
}
public void setDr(Draw dr) {
this.dr = dr;
}
}
class Main.javapublic class Triangle {
private Point point1;
private Draw dr;
public Point getPoint1() {
return point1;
}
public void setPoint1(Point point1) {
this.point1 = point1;
}
public void show()
{
System.out.println("Ponit1 x="+point1.getX()+"y="+point1.getY());
dr.draw();
}
public Draw getDr() {
return dr;
}
public void setDr(Draw dr) {
this.dr = dr;
}
}
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Triangle tri=(Triangle) context.getBean("triangle");
tri.show();
}
}
spring.xmlimport org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Triangle tri=(Triangle) context.getBean("triangle");
tri.show();
}
}
<?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="point" class="com.test.Point">
<property name="x" value="10"></property>
<property name="y" value="20"></property>
</bean>
<bean id="draw" class="com.test.Draw">
</bean>
<bean id="triangle" class="com.test.Triangle" autowire="byType">
</bean>
</beans>
Ponit1 x=10y=20
Triangle Drawn
Triangle Drawn
For Further Reading,
0 comments:
Post a Comment