How to do it...

With ch02-xml, let us demonstrate how Spring loads objects using the XML-based ApplicationContext container:

  1. Create a package org.packt.starter.ioc.model, where our model classes will be placed. Our model classes will be typical Plain Old Java Objects (POJO), for which the Spring 5.0 architecture is known.
  2. Inside the newly created package, create the classes Employee and Department, which contain the following blueprints:
public class Employee { 
    
   private String firstName; 
   private String lastName; 
   private Date birthdate; 
   private Integer age; 
   private Double salary; 
   private String position; 
   private Department dept; 
    
   public Employee(){ 
      System.out.println(" an employee is created."); 
   } 
 
   public Employee(String firstName, String lastName, Date 
          birthdate, Integer age, Double salary, String position, 
         Department dept) { 
     his.firstName = firstName; 
     his.lastName = lastName; 
     his.birthdate = birthdate; 
     his.age = age; 
     his.salary = salary; 
     his.position = position; 
     his.dept = dept; 
     System.out.println(" an employee is created."); 
      } 
      // getters and setters 
   } 
 
public class Department { 
    
   private Integer deptNo; 
   private String deptName; 
    
   public Department() { 
      System.out.println("a department is created."); 
   } 
   // getters and setters 
} 
  1. Afterwards, open the ApplicationContext implemented as ch02-beans.xml. Register using the <bean> tag our first set of Employee and Department objects as follows:
<bean id="empRec1" class="org.packt.starter.ioc.model.Employee" /> 
<bean id="dept1" class="org.packt.starter.ioc.model.Department" /> 
  1. The beans in Step 3 contain private instance variables that have zeroes and null default values. To update them, our classes have mutators or setter methods that can be used to avoid the NullPointerException that is always encountered whenever empty objects undergo transactions. In Spring, calling these setters is tantamount to injecting data into the <bean>, similarly to how the following objects are created:
<bean id="empRec2" class="org.packt.starter.ioc.model.Employee"> 
  <property name="firstName"><value>Juan</value></property> 
  <property name="lastName"><value>Luna</value></property> 
  <property name="age"><value>70</value></property> 
  <property name="birthdate">
<value>October 28, 1945</value>
</property> <property name="position"> <value>historian</value>
</property> <property name="salary"><value>150000</value></property> <property name="dept"><ref bean="dept2"/></property> </bean> <bean id="dept2" class="org.packt.starter.ioc.model.Department"> <property name="deptNo"><value>13456</value></property> <property name="deptName"> <value>History Department</value>
</property> </bean>
  1. A <property> tag is equivalent to a setter definition accepting an actual value or an object reference. The name attribute defines the name of the setter minus the prefix set with the conversion to its camel-case notation. The value attribute or the <value> tag both pertain to supported Spring-type values (for example, int, double, float, Boolean, Spring). The ref attribute or <ref> provides a reference to another loaded <bean> in the container. Another way of writing the bean object empRec2 is through the use of ref and value attributes such as the following:
<bean id="empRec3" class="org.packt.starter.ioc.model.Employee"> 
  <property name="firstName" value="Jose"/> 
  <property name="lastName" value="Rizal"/> 
  <property name="age" value="101"/> 
  <property name="birthdate" value="June 19, 1950"/> 
  <property name="position" value="scriber"/> 
  <property name="salary" value="90000"/> 
  <property name="dept" ref="dept3"/> 
</bean> 
<bean id="dept3" class="org.packt.starter.ioc.model.Department"> 
  <property name="deptNo" value="56748"/> 
  <property name="deptName" value="Communication Department" /> 
</bean> 
  1. Another way of updating the private instance variables of model objects is to make use of constructors. Actual Spring data and object references can be inserted to the <bean> through the <contructor-arg> metadata:
<bean id="empRec5" class="org.packt.starter.ioc.model.Employee"> 
  <constructor-arg><value>Poly</value></constructor-arg> 
  <constructor-arg><value>Mabini</value></constructor-arg> 
  <constructor-arg><value>August 10, 1948</value></constructor-arg> 
  <constructor-arg><value>67</value></constructor-arg> 
  <constructor-arg><value>45000</value></constructor-arg> 
  <constructor-arg><value>Linguist</value></constructor-arg> 
  <constructor-arg><ref bean="dept3"></ref></constructor-arg> 
</bean>
  1. After all the modifications, save ch02-beans.xml. Create a TestBeans class inside org.packt.starter.ioc.model.test of the src estjava directory. This class will load the XML configuration resource to the ApplicationContext container through org.springframework.context.support.ClassPathXmlApplicationContext and will fetch all the objects created through its getBean() method:
public class TestBeans { 
   public static void main(String args[]){ 
       
      ApplicationContext context = new 
             ClassPathXmlApplicationContext("ch02-beans.xml"); 
       System.out.println("application context loaded."); 
        
       System.out.println("****The empRec1 bean****"); 
       Employee empRec1 = (Employee) context.getBean("empRec1"); 
       
       System.out.println("****The empRec2*****"); 
       Employee empRec2 = (Employee) context.getBean("empRec2"); 
       Department dept2 = empRec2.getDept(); 
       System.out.println("First Name: " + 
                        empRec2.getFirstName()); 
       System.out.println("Last Name: " + empRec2.getLastName()); 
       System.out.println("Birthdate: " + 
                        empRec2.getBirthdate()); 
       System.out.println("Salary: " + empRec2.getSalary()); 
       System.out.println("Dept. Name: " + dept2.getDeptName()); 
        
       System.out.println("****The empRec5 bean****"); 
       Employee empRec5 = context.getBean("empRec5", 
                        Employee.class); 
       Department dept3 = empRec5.getDept(); 
       System.out.println("First Name: " + 
                        empRec5.getFirstName()); 
       System.out.println("Last Name: " + empRec5.getLastName()); 
       System.out.println("Dept. Name: " + dept3.getDeptName()); 
   }  
} 
  1. The expected output after running the main() thread will be:
an employee is created. 
an employee is created. 
a department is created. 
an employee is created. 
a department is created. 
an employee is created. 
a department is created. 
application context loaded. 
*********The empRec1 bean *************** 
*********The empRec2 bean *************** 
First Name: Juan 
Last Name: Luna 
Birthdate: Sun Oct 28 00:00:00 CST 1945 
Salary: 150000.0 
Dept. Name: History Department 
*********The empRec5 bean *************** 
First Name: Poly 
Last Name: Mabini 
Dept. Name: Communication Department 
..................Content has been hidden....................

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