Now, how does it work with Class variables value setting using Spring?
This can happen at 2 levels in Spring,
1)Getter and Setter methods of the variable
2)Using Class Constructors.
The DrawingApp class remains the same,
public class DrawingApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle = (Triangle) context.getBean("triangle");
triangle.Draw();
}
}
1)Lets look into the getters and setters of the variable Check the code below:
Implement 2 variables in Triangle class as below:
public class Triangle {
private String type;
private int height;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public void Draw() {
System.out.println(getType() + "Triangle Drawn");
System.out.println(getHeight() + "is the height");
}
}
Now, in order to set these variables using the spring framework, we need to implement the <property> tag in spring.xml as below:
<beans>
<bean id="triangle" class="com.project.springDemo.Triangle">
<property name="type" value="equilateral"/>
<property name="height" value="20"/>
</bean>
</beans>
note: There can be more than one bean tag within the beans tag.
And now, while executing the DrawingApp Class, the Spring framework uses the setters methods to set the values of the variables to the value attribute based on the name value of the <property> tag in spring.xml . Note: The value of the name attribute should be equal to the variable name in the POJO.
2)Lets look into the Constructor arguments way of setting variable. Check the code below:
Implement 2 variables in Triangle class along with constructors as below:
public class Triangle {
private String type;
private int height;
public Triangle(String type,int height) ------(a)
{
this.type = type;
this.height=height;
}
public Triangle(String type)-----------------(b)
{
this.type = type;
}
public String getType() {
return type;
}
// public void setType(String type) {
// this.type = type;
// }
public int getHeight() {
return height;
}
// public void setHeight(int height) {
// this.height = height;
// }
public void Draw()
{
System.out.println(getType() + " Triangle Drawn");
System.out.println(getHeight() + " is the height");
}
}
Note that the setters are no more required, as the spring framework uses the Constructors to set them in this scenario.
Now, in order to set these variables using the spring framework, we need to implement the <constructor-arg> tag in spring.xml :
some scenarios of using the <constructor-arg> are:
note: The name attribute is not there in this tag.
(i)----------Sets class variable using constructor (a)--------------------------------------------------
<beans>
(ii)----------Sets class variable using constructor (a), same as (i)-----------------------------------
<beans>
(iii)-------Sets class variable using constructor (b)-----------------------------------------------------
<beans>
This can happen at 2 levels in Spring,
1)Getter and Setter methods of the variable
2)Using Class Constructors.
The DrawingApp class remains the same,
public class DrawingApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle = (Triangle) context.getBean("triangle");
triangle.Draw();
}
}
1)Lets look into the getters and setters of the variable Check the code below:
Implement 2 variables in Triangle class as below:
public class Triangle {
private String type;
private int height;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public void Draw() {
System.out.println(getType() + "Triangle Drawn");
System.out.println(getHeight() + "is the height");
}
}
Now, in order to set these variables using the spring framework, we need to implement the <property> tag in spring.xml as below:
<beans>
<bean id="triangle" class="com.project.springDemo.Triangle">
<property name="type" value="equilateral"/>
<property name="height" value="20"/>
</bean>
</beans>
note: There can be more than one bean tag within the beans tag.
And now, while executing the DrawingApp Class, the Spring framework uses the setters methods to set the values of the variables to the value attribute based on the name value of the <property> tag in spring.xml . Note: The value of the name attribute should be equal to the variable name in the POJO.
2)Lets look into the Constructor arguments way of setting variable. Check the code below:
Implement 2 variables in Triangle class along with constructors as below:
public class Triangle {
private String type;
private int height;
public Triangle(String type,int height) ------(a)
{
this.type = type;
this.height=height;
}
public Triangle(String type)-----------------(b)
{
this.type = type;
}
public String getType() {
return type;
}
// public void setType(String type) {
// this.type = type;
// }
public int getHeight() {
return height;
}
// public void setHeight(int height) {
// this.height = height;
// }
public void Draw()
{
System.out.println(getType() + " Triangle Drawn");
System.out.println(getHeight() + " is the height");
}
}
Note that the setters are no more required, as the spring framework uses the Constructors to set them in this scenario.
Now, in order to set these variables using the spring framework, we need to implement the <constructor-arg> tag in spring.xml :
some scenarios of using the <constructor-arg> are:
note: The name attribute is not there in this tag.
(i)----------Sets class variable using constructor (a)--------------------------------------------------
<beans>
<bean id="triangle" class="com.project.springDemo.Triangle">
<constructor-arg index="0" value="equilateral"/>
<constructor-arg index="1" value="20"/>
</bean>
</beans>
(ii)----------Sets class variable using constructor (a), same as (i)-----------------------------------
<beans>
<bean id="triangle" class="com.project.springDemo.Triangle">
<constructor-arg type="java.lang.String" value="equilateral"/>
<constructor-arg type="int" value="20"/>
</bean>
</beans>
(iii)-------Sets class variable using constructor (b)-----------------------------------------------------
<beans>
<bean id="triangle" class="com.project.springDemo.Triangle">
<constructor-arg type="java.lang.String" value="equilateral"/>
</bean>
</beans>
No comments:
Post a Comment