How to do it...

Perform the following to create inner beans:

  1. In the ch02-xml, inject an Employee object, applying method injection for the actual values:
<bean id="empRec4" class="org.packt.starter.ioc.model.Employee"> 
  <property name="firstName" value="Gabriela"/> 
  <property name="lastName" value="Silang"/> 
  <property name="age" value="67"/> 
  <property name="birthdate" value="June 19, 1950"/> 
  <property name="position" value="writer"/> 
  <property name="salary" value="897000"/> 
</bean> 
  1. Let us wire a Department object to the empRec4 object. Instead of using <ref/> to wire any Department object in the container, one option is to use a feature similar to an anonymous inner class and that is inner beans. The usual <bean> metadata is still used to create inner beans but without the id and class attributes. Also, an inner bean does not need a scope attribute because it is bounded within the scope of its top-level object. The following is the full-blown empRec4 bean with a Department object created as an inner bean:
<bean id="empRec4" class="org.packt.starter.ioc.model.Employee"> 
  <property name="firstName" value="Gabriela"/> 
  <property name="lastName" value="Silang"/> 
  <property name="age" value="67"/> 
  <property name="birthdate" value="June 19, 1950"/> 
  <property name="position" value="writer"/> 
  <property name="salary" value="897000"/> 
  <property name="dept"> 
    <bean class="org.packt.starter.ioc.model.Department" 
scope="Prototype"> <property name="deptNo" value="48574"/> <property name="deptName" value="Humanities Department"/> </bean> </property> </bean>
  1. In the case of the ch02-jc project, the JavaConfig approach allows the creation of an anonymous inner class at the argument body of the setter method. To create an Employee bean with an inner bean Department, add the following snippet in the BeanConfig definition:
@Bean(name="empRec5") 
public Employee getEmpRecord5(){ 
    Employee empRec5 = new Employee(); 
    empRec5.setFirstName("Gabriela"); 
    empRec5.setLastName("Silang"); 
    empRec5.setAge(67); 
    empRec5.setBirthdate(new Date(50,5,19)); 
    empRec5.setPosition("writer"); 
    empRec5.setSalary(89700.00); 
    empRec5.setDept(new Department(){ 
        String deptName = "Communication Department"; 
        Integer deptNo = 232456; 
          
        @Override 
        public String getDeptName() { 
            // TODO Auto-generated method stub 
           return deptName; 
        } 
          
        @Override 
        public Integer getDeptNo() { 
            // TODO Auto-generated method stub 
            return deptNo; 
        } 
        
        @Override 
        public void setDeptName(String deptName) { 
            his.deptName = deptName; 
        } 
          
        @Override 
        public void setDeptNo(Integer deptNo) { 
            his.deptNo = deptNo; 
        } 
    }); 
    return empRec5; 
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.31.165