How to inject value into spring bean instance variables?
We can inject values into spring bean from spring bean configuration file itself. It is very simple. This page shows how to
populate values to spring bean instance variables. Here we have Employee class, which has three fields called name, role, employee id:
package com.java2novice.beans;
public class Employee {
private int empId;
private String name;
private String role;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
|
Here is the spring bean xml based configuration file, using property tag, we are injecting the values:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="employeeBean" class="com.java2novice.beans.Employee">
<property name="empId" value=""1016 />
<property name="name" value="Nataraj" />
<property name="role" value="Manager" />
</bean>
</beans>
|
|