Thursday, May 24, 2012

Spring Framework- Constructor argument with indexing


In this example describe that we can define constructor argument with index numbers in xml file. which in define clearly in example.
Directory  structure of project-
Class Triangle.java
package com.test;

public class Triangle {

private String type;
private int height;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public Triangle(String type,int height)
{
this.type=type;
this.height=height;
}

public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void draw()
{
System.out.println( getType()+"Triangle.\n The height is="+getHeight() );
}
}

Class DrawApp.java
package com.test;

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

public class DrawApp {

public static void main(String a[])
{
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Triangle tri= (Triangle) context.getBean("triangle");
tri.draw();
}
}

xml file 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 id="triangle" class="com.test.Triangle">
<constructor-arg  index="0" value="equilateral"/>
<constructor-arg index="1" value="10"/>
</bean>
</beans>


the output is-
equilateral Triangle.
The height is=10


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.