Reading constant from properties file
constant.properties
PointA.PointX=-20 PointA.PointY=0
spring.xml
<beans>
<bean id="pointA" class="com.mugil.shapes.Point">
<property name="x" value="${PointA.PointX}"/>
<property name="y" value="${PointA.PointY}"/>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="constant.properties"></property>
</bean>
</beans>
Using Interface
spring.xml
<beans>
<bean id="pointA" class="com.mugil.shapes.Point">
<property name="x" value="${PointA.PointX}"/>
<property name="y" value="${PointA.PointY}"/>
</bean>
<bean class="com.mugil.shapes.Circle" id="circleId">
<property name="center" ref="pointA"/>
</bean>
</beans>
shape.java
public interface Shape {
public void drawShape();
}
triangle.java
public class Triangle implements Shape
{
public void drawShape()
{
System.out.println("Shape of Triangle");
}
}
circle.java
public class Circle implements Shape{
private Point center;
@Override
public void drawShape() {
System.out.println("Shape of Circle ");
System.out.println("Center of Cirlce "+ getCenter().getX() + " - " + getCenter().getY());
}
public Point getCenter() {
return center;
}
public void setCenter(Point center) {
this.center = center;
}
}
The objShape will call the draw method based on the instance referenced at runtime
shape.java
public class DrawingApp {
public static void main(String[] args) {
ApplicationContext objContext = new ClassPathXmlApplicationContext("spring1.xml");
Shape objShape = (Shape)objContext.getBean("circleId");
objShape.drawShape();
}
}