Recent Posts

Saturday, 8 June 2019

Steps to prepare Spring application

     To prepare spring application we need following things,
1. Spring Bean class
     The main intention of Bean classes in Spring applications is to manage properties and their setXXX() and getXXX() methods and some other Business Methods. To prepare Bean classes in Spring applications we have to use the following Guidelines.
☀ Bean classes must be POJO classes, they must not extend or implement any predefined Library except java.io.Serializable marker interface.
☀ Bean must be declared as "public", "Non-abstract" and "non-final". The main intention of declaring bean class as "public" is to make available bean class scope to IOC Container in order to create objects. The main intention to declare bean class as "Non-abstract" is to allow to create object. The main intention to declare bean classes as "Non-final" is to extend one bean class to another bean class in order to improve reusability.
☀ In Bean classes, we have to declare all properties as "private" and all behavior's as "public", it will improve "Encapsulation".
☀ If we want to provide any constructor in bean class then provide a constructor, it must be 0-arg constructor and "public" constructor, because, IOC Container will search and execute public and 0-arg constructor while instantiating bean. 

2. Bean Configuration File
     The main intention of Bean Configuration File is to provide all the bean components configuration details like logical name, fully qualified names of the bean classes, bean classes properties and bean classes dependencies etc. to the IOC container in order to create bean objects and their dependent objects.
Bean Configuration File is an XML file, it will use the following XML tags to configure bean classes.
E.g.
<beans ----XSD----- >
   -----
   <bean id="--" class="---">
      -----
   </bean>
  -----
</beans>
Where "<beans>" tag is a root tag, it will include no of beans configurations.
Where "<bean>" tag can be used to configure single bean class.
Where "id" attribute will take identity name or logical name to the Bean component.
Where "name" attribute will take fully qualified name of the bean class.

3. Client Application
     The main intention of Test / Client application is to activate IOC Container, to create Bean Components and to access business methods. If we provide properties and their respective setXXX() and getXXX() methods in bean classes then we have to send values to bean properties in the following two ways.
1) From Test Program
2) From Beans configuration File.
1. From Test Program
     In this approach, we have to set value to bean properties programmatically from Test application. E.g.
package com.ashok.spring.core.basics.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private String empAddress;
   private double salary;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public String getEmpAddress() {
      return empAddress;
   }

   public void setEmpAddress(String empAddress) {
      this.empAddress = empAddress;
   }

   public double getSalary() {
      return salary;
   }

   public void setSalary(double salary) {
      this.salary = salary;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + ", salary=" + salary
            + "]";
   }
}
Config file
<?xml version="1.0" encoding="UTF-8"?>
<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 = "emp" class = "com.ashok.spring.core.basics.beans.Employee"/>
</beans>
Client Application
package com.ashok.spring.core.basics.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ashok.spring.core.basics.beans.Employee;
public class TestSpringApplication {
   @SuppressWarnings("resource")
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/core/basics/config/beans.xml";
      ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      Employee emp = (Employee) context.getBean("emp");
      emp.setEmpName("Ashok Kumar");
      emp.setEmpId("E0087");
      emp.setEmpAddress("Bhimavaram");
      emp.setSalary(45000);
      System.out.println(emp);
   }
}
     In the above approach, we have to recompile the Test application when we change messages, it is not suggestible in application development. To overcome this problem, we have to use beans configuration file to prepare messages in order to send to Bean object.

2. Beans Configuration File
     To provide messages to the bean properties through their setXXX() methods from bean configuration file we have to use the following tag in beans configuration file.
<beans ---XSD----- >
   ----
   <bean id="--" class="--">
      <property name="--" value="--"/>
      ----
     </bean>
   ----
</beans>
     Where <property> tag is able to represent single bean property. Where "name" attribute in <property> tag is able to take property name to which we want to send data. Where "value" attribute in <property> tag is able to take the value which we want to send to the bean property.
E.g.
package com.ashok.spring.core.basics.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private String empAddress;
   private double salary;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public String getEmpAddress() {
      return empAddress;
   }

   public void setEmpAddress(String empAddress) {
      this.empAddress = empAddress;
   }

   public double getSalary() {
      return salary;
   }

   public void setSalary(double salary) {
      this.salary = salary;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + ", salary=" + salary
            + "]";
   }
}
Config File
<?xml version="1.0" encoding="UTF-8"?>
<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 = "emp" class = "com.ashok.spring.core.basics.beans.Employee">
      <property name = "empName" value = "Ashok Kumar"/>
      <property name = "empId" value = "Emp0087"/>
      <property name = "empAddress" value = "Bhimavaram"/>
      <property name = "salary" value = "45000"/>
   </bean>
</beans>
Client Application
package com.ashok.spring.core.basics.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ashok.spring.core.basics.beans.Employee;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   @SuppressWarnings("resource")
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/core/basics/config/beans.xml";
      ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      Employee emp = (Employee) context.getBean("emp");
      System.out.println(emp);
   }
}

Next Tutorial : Spring IOC Containers

Previous Tutorial : What is Spring Framework
 

No comments:

Post a Comment