Steps to prepare Spring application
In this tutorial, we are going to discuss Prepare Spring Application.
To prepare the spring application we need the 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, and 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 bean class scope available to IOC Container to create objects. The main intention to declare the bean class as “Non-abstract” is to create an object. The main intention to declare bean classes as “Non-final” is to extend one bean class to another bean class to improve reusability.
- In Bean classes, we have to declare all properties as “private” and all behaviour’s as “public”, and it will improve “Encapsulation”.
- If we want to provide any constructor in the bean class, then provide a constructor. It must be a 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 the 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 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.
<beans ----XSD----- >
-----
<bean id="--" class="---">
-----
</bean>
-----
</beans>
- The “<beans>” tag is a root tag. It will include no of beans configurations.
- The “<bean>” tag can be used to configure a single bean class.
- The “id” attribute will take the identity name or logical name to the Bean component.
- The “name” attribute will take the fully qualified name of the bean class.
3. Client Application
The main intention of the Client application is to activate IOC Container, create Bean Components, and access business methods. If we provide properties and their respective setXXX() and getXXX() methods in bean classes, we have to send values to bean properties in the following two ways.
- From Test Program
- 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;
/**
*
* @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");
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 a beans configuration file to prepare messages to send to the Bean object.
2. Beans Configuration File
To provide messages to the bean properties through their setXXX() methods from the bean configuration file, we have to use the following tag in the beans configuration file.
<beans ---XSD----- >
----
<bean id="--" class="--">
<property name="--" value="--"/>
----
</bean>
----
</beans>
Where,
- A bean tag can represent single bean property.
- The “name” attribute in the tag can take the property name we want to send data.
- The “value” attribute in the tag can take the value 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);
}
}