·
Int, String,
float all these data types are comes under Primitive Data
·
We use <property/> tag , to do setter DI
·
At runtime the
metadata from xml will inject into bean class by DI
·
If we not pass
any data through <property > the container will create def. constructor
·
Setter method
values can pass by using value attribute or <value > child tag
·
we should not
pass duplicate attribute values
·
we must pass
name attribute
Syntax
If
Student is bean with name as String data type, then the Syntax will be
<beans> <bean id="ob" class="bean.setter.StudentBO"> <property name="sno" value="100"></property> <property name="name"
value="Kaveti Satya"></property> </bean> </beans> |
Name à variable name
Value à value to be inject at
runtime
Example
//StudentBo.java
package bean.setter; public class StudentBO { private int sno; private String name; public int getSno() { return sno; } public void setSno(int sno) { this.sno = sno; } public String getName() { return name; } public void setName(String
name) { this.name = name; } public void getData(){ System.out.println("********** DATA
**********"); System.out.println("Sno --> "+getSno()+"\n
Name ---> "+getName()); } } |
//SetterMain.java
package main; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import bean.setter.StudentBO; public class SetterMain { public static void main(String[] args)
{ Resource resource = new ClassPathResource("res/setter.xml"); BeanFactory factory = new XmlBeanFactory(resource); StudentBO bo = (StudentBO) factory.getBean("ob"); bo.getData(); } } |
// res/setter.xml
<!DOCTYPE
beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean
id="ob" class="bean.setter.StudentBO"> <property
name="sno" value="100"></property> <property
name="name" value="Kaveti Satya"></property> </bean> </beans> |
//Output
Output log4j:WARN Please initialize the log4j system properly. Picked up
_JAVA_OPTIONS: -Djava.net.preferIPv4Stack=false ********** DATA
********** Sno ---> 100 Name ---> Kaveti Satya |
Cases
1.
If we not pass any data through <property >
the container will create def. constructor
package bean.setter;
public class StudentBO
{
public StudentBO()
{
System.out.println("**Def.
constructor***");
}
private int sno;
private String name;
public int getSno()
{
return sno;
}
public void setSno(int sno)
{
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String
name) {
this.name = name;
}
public void getData(){
System.out.println("********** DATA
**********");
System.out.println("Sno ---> "+getSno()+"\n Name ---> "+getName());
}
}
package main;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import bean.setter.StudentBO;
public class SetterMain
{
public static void main(String[] args) {
Resource resource
= new ClassPathResource("res/setter.xml");
BeanFactory factory = new XmlBeanFactory(resource);
StudentBO bo
= (StudentBO) factory.getBean("ob");
bo.getData();
}
}
<beans>
<bean id="ob" class="bean.setter.StudentBO">
<!-- here we are not
passing any data -->
</bean>
</beans>
Output
log4j:WARN
Please initialize the log4j system properly.
**Def. constructor***
********** DATA
**********
Sno ---> 0
Name ---> null
Picked
up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=false
2. Setter method values can pass by
using value attribute or <value > child tag
<beans> <bean id="ob" class="bean.setter.StudentBO"> <property name="sno"> <value>100</value> </property> <property name="name"> <value>Satya</value> </property> </bean> </beans> |
3.
We should not pass duplicate attribute values
<beans> <bean id="ob" class="bean.setter.StudentBO"> <property name="sno" value="100"></property> <property name="sno" value="100"></property> <property name="name"
value="Kaveti Staya"></property> </bean> </beans> |
Output
***********************************************************
Bean 'ob';
nested exception is
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Multiple
'property' definitions for property 'sno'
Offending resource: class path resource
[res/setter.xml]
Bean 'ob'
Property 'sno'
at
org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
4. We must pass name attribute
<beans> <bean id="ob" class="bean.setter.StudentBO"> <property value="100"></property> <property name="name"
value="Kaveti Staya"></property> </bean> </beans> |
Output
***********************************************************
Exception in thread "main"
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Attribute "name" is required
and must be specified for element type "property".