In Spring framework, whenever a bean is used for only one particular
property, it’s advise to declare it as an inner bean. And the inner bean
is supported both in setter injection ‘
property
‘ and constructor injection ‘constructor-arg
‘.
See a detail example to demonstrate the use of Spring inner bean.
DrawingApp.java
public class DrawingApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Circle circle = (Circle) context.getBean("circle");
circle.draw();
}
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Circle circle = (Circle) context.getBean("circle");
circle.draw();
}
}
Circle.java
public class Circle {
private Point center;
public Point getCenter() {
return center;
}
public void setCenter(Point center) {
this.center = center;
}
public void draw() {
System.out.println("Center is (" +center.getX()+","+center.getY()+")");
}
}
Point.java
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;
}
}
public Point getCenter() {
return center;
}
public void setCenter(Point center) {
this.center = center;
}
public void draw() {
System.out.println("Center is (" +center.getX()+","+center.getY()+")");
}
}
Point.java
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;
}
}
Often times, you may use ‘
ref
‘ attribute to reference the “Point” bean into “Circle” bean, person property as following :
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">
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="circle" class="com.example2.Circle">
<property name="center" ref="centerpoint" />
</bean>
<bean id="centerpoint" class="com.example2.Point">
<property name="x" value="0" />
<property name="y" value="0" />
</bean>
</beans>
<bean id="circle" class="com.example2.Circle">
<property name="center" ref="centerpoint" />
</bean>
<bean id="centerpoint" class="com.example2.Point">
<property name="x" value="0" />
<property name="y" value="0" />
</bean>
</beans>
In general, it’s fine to reference like this, but since the
point bean is only used for Circle bean only, it’s better to declare
this Point Bean as an inner bean as following
<?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="circle" class="com.example2.Circle">
<property name="center">
<bean class="com.example2.Point">
<property name="x" value="0" />
<property name="y" value="0" />
</bean>
</property>
</bean>
</beans>
Output : Center is (0,0)
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="circle" class="com.example2.Circle">
<property name="center">
<bean class="com.example2.Point">
<property name="x" value="0" />
<property name="y" value="0" />
</bean>
</property>
</bean>
</beans>
Output : Center is (0,0)
No comments:
Post a Comment