How to do it...

Perform the following steps to auto-wire Collections and Properties components:

  1. For both of the projects involved in the preceding recipes, create the following model classes inside their own package org.packt.starter.ioc.model:
public class ListEmployees { 
    
   private List<Employee> listEmps; 
   private List<String> listEmpNames; 
 
// getters and setters 
} 
public class SetDepartments { 
    
   private Set<Department> setDepts; 
   private Set<String> deptNames; 
 
// getters and setters 
} 
public class MapEmpTasks { 
   private Map<String, Employee> mapEmpTask; 
private Map<String,String> mapEmpMgr; 
 
// getters and setters 
} 
 
public class PropertiesAudition { 
   private Properties auditionAddress; 
private Properties auditonRequirement; 
 
// getters and setters 
} 
  1. Each model class must contain Collections and Properties instance objects with their respective setter methods to be used later for method dependency injection.
  2. In ch02-xml, use the <list> tag to inject required object references and actual values to setListEmps() and setListEmpNames().Within the <list> tag is a series of <value> and <ref> metadata, where <value> holds the typical Spring-supported types such as string, int, double and other primitive types while <ref> holds references to other beans:
<bean id="listEmployees" 
   class="org.packt.starter.ioc.model.ListEmployees"> 
      <property name="listEmps"> 
         <list> 
              <ref bean="empRec2"/> 
              <ref bean="empRec3"/> 
              <ref bean="empRec4"/> 
         </list> 
      </property> 
      <property name="listEmpNames"> 
         <list> 
              <value>Juan</value> 
              <value>Jose</value> 
         </list> 
      </property> 
</bean> 
  1. Let us use the <set> tag to inject Department objects to setDepts() and department names to deptNames() for the SetDepartments bean. Just like <list>, the <set> metadata contains a number of <value> or <ref> tags:
<bean id="setDepartments" 
   class="org.packt.starter.ioc.model.SetDepartments"> 
      <property name="setDepts"> 
         <set> 
              <ref bean="dept2"/> 
              <ref bean="dept3"/> 
              <ref bean="dept4"/> 
         </set> 
      </property> 
      <property name="deptNames"> 
         <set> 
              <value>Music</value> 
              <value>Arts</value> 
         </set> 
      </property> 
</bean> 
  1. Inject Map objects to any bean properties in the Spring container using the <map> tag and add within it a series of <entry> metadata containing the key and value of an Entry. The following code injects tasks (key) to Employee records (Object value) and managers (String value):
<bean id="mapEmpTasks" 
   class="org.packt.starter.ioc.model.MapEmpTasks"> 
      <property name="mapEmpTask"> 
          <map> 
              <entry key="expository"> 
                   <ref bean="empRec2"/> 
              </entry> 
              <entry key="feature" value-ref="empRec3"/> 
                 
          </map> 
      </property> 
      <property name="mapEmpMgr"> 
          <map> 
              <entry key="expository"> 
                  <value>Joan Arkos</value> 
              </entry> 
              <entry key="feature" value="Billy Jean"/> 
          </map> 
      </property> 
</bean> 
  1. Properties objects are used to store configuration details and information for initialization instance and class variables, database connections, or e-mail transactions. This recipe has a custom model, PropertiesAudition, which simply provides information for an audition request. To inject a Properties object to the Spring container, we use the <props> tag with a series of <value> or <prop> metadata that holds the key and value of a property. Add the following code, which injects entries to the properties of PropertiesAudition:
<bean id="auditionInfo" 
   class="org.packt.starter.ioc.model.PropertiesAudition"> 
       <property name="auditionAddress"> 
            <value> 
                 country=Philippines 
                 city=Makati 
                 building=Rufino Tower 2 
                 zipcode=1233   
            </value> 
       </property> 
       <property name="auditionRequirement"> 
            <props> 
                <prop key="document">curriculum vitae</prop> 
                <prop key="picture">2x2 recent picture</prop> 
                <prop key="time">8:00 AM</prop> 
            </props> 
       </property> 
</bean> 
  1. In our ch02-jc project, use the simple and straightforward JavaConfig way of using the @Bean annotation as shown by the following injection:
@Bean 
public ListEmployees listEmployees(){ 
    ListEmployees listEmps = new ListEmployees(); 
       
    List<Employee> empRecs = new ArrayList<>(); 
    // refer to sources 
    listEmps.setListEmps(empRecs); 
       
    List<String> empNames = new ArrayList<>(); 
    // refer to sources 
    listEmps.setListEmpNames(empNames); 
       
    return listEmps; 
} 
    
    
@Bean 
public SetDepartments setDepartments(){ 
    SetDepartments setDepts = new SetDepartments(); 
       
    Set<Department> deptRecs = new HashSet<>(); 
    // refer to sources 
    setDepts.setSetDepts(deptRecs); 
    
    Set<String> deptNames = new HashSet<>(); 
    // refer to sources 
    setDepts.setDeptNames(deptNames); 
    return setDepts; 
} 
    
@Bean 
public MapEmpTasks mapEmpTasks(){ 
    MapEmpTasks mapTasks = new MapEmpTasks(); 
       
    Map<String,Employee> empTasks = new HashMap<>(); 
    // refer to sources 
    mapTasks.setMapEmpTask(empTasks); 
       
    Map<String, String> mgrTasks = new HashMap<>(); 
    // refer to sources 
    mapTasks.setMapEmpMgr(mgrTasks); 
    return mapTasks; 
} 
    
@Bean 
public PropertiesAudition auditionInfo(){ 
    PropertiesAudition auditionInfo = new PropertiesAudition(); 
       
    Properties addressProps = new Properties(); 
    // refer to sources 
    auditionInfo.setAuditionAddress(addressProps); 
       
    Properties reqtProps = new Properties(); 
    // refer to sources 
    auditionInfo.setAuditionRequirement(reqtProps); 
       
    return auditionInfo; 
} 
  1. Create a test class, TestInjectData, which retrieves all the bean models with the data injected using the getBean() method.