Java Beans
Java Bean is a Reusable component, it is able to manage the state of a particular entity.
Java Bean is a simple Java class, it must have properties, default constructor and the respective setter and getter methods.
In general, Java bean classes are available in the following forms in the different technologies and tools.
Struts ———-> ActionForms
JSF ————-> Backing Bean
EJBs————-> Entity Bean, Session Bean,….
Hibernate ——-> Entity Bean , POJO class.
Spring ———-> POJO class.
To prepare bean components in the Spring applications we have to use the following guidelines.
- In Spring based applications, Every bean component must be a POJO[Plain Old Java Object] class, it is a normal java bean class, it must not implement or extend any predefined library[classes and interfaces].
Note: It is exceptional for the java.io.Serializable marker interface.
- In Spring based applications, every Java bean class must be a public, non abstract and a non final class.
- The main purpose of declaring bean class as public is to bring bean class scope to the IOContainer in order to create objects.
- The main purpose of declaring bean class as a non abstract class is to allow the creation of objects for the bean classes.
- In the applications, there is a requirement to extend one bean class to another bean class to improve code reusability, here to extend one bean class to another bean class the respective bean classes must not be declared as final.
- In bean classes, every property must be declared as private and every method or behavior must be declared as public in order to improve Encapsulation.
- In bean classes, for every property we must provide a separate setXXX() method and getXXX() method.
- In Java bean classes, it is suggested to override the Object class provided equals() method in the bean class in order to compare two bean objects on the basis of a particular parameter.
- In Java bean classes, it is suggested to override hashcode() method in order to provide hashcode values as per the requirement.
- In javabean components, it is suggested to override the toString() method in order to display our own data in place of “ClassName@RefValue”.
Spring Configurations – approaches ?
In Spring based applications, we are not creating objects for the bean classes, IOC Containers will create objects for the bean classes, here to create objects for the bean classes by the Containers , first containers must have the information or metadata of the bean classes.
In Spring based applications there are three ways to provide bean metadata or description to the IOC Containers.
- XML Based Configurations
- Java Based Configurations
- Annotation Based Configurations
XML Based Bean Configurations
If we want to provide bean configurations through XML based configurations we have to prepare a Spring configuration file , where we have to use the following tags to configure the bean classes.
<?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.xsd">
—----
<bean id=”---” name=”--” class=”--” scope=”--”>
—-----
</bean>
—-----
</beans>
Where the <beans> tag is a root tag in the Spring configuration file, it is able to include the number of bean classes configurations.
Where the <bean> tag is able to represent a single bean class configuration.
Where the “id” attribute in the <bean> tag is able to take an identity of the Bean object. By using this id attribute value only we are able to get the Bean object from the container.
Where the “name” attribute in the <bean> tag is able to take an identity of the Bean object. By using this name attribute value only we are able to get the Bean object from the container.
Q) What is the difference between “id” attribute and “name” attribute in the <bean> tag in the Spring configuration file?
In the Spring configuration configuration file , in the bean configuration both id and name attributes will provide identity value to the beans objects.
‘id’ attribute is able to allow only one unique identity for the bean object.
‘name’ attribute is able to allow more than identity values to the bean objects.
To the name attribute we will provide more than one value by using the separators like space, ;, ,.
Note: in the case of name attribute if we provide more than one value then the first value is treated as the identity value and the remaining values are treated as the alias names to the first value.
Examples of bean definitions – id & name fields
<bean id=”emp1” class=”com.durgasoft.beans.Employee”/>
Employee employee = (Employee)applicationContext.getBean(“emp1”);
<bean id=”emp1emp2emp3” class=”com.durgasoft.beans.Employee”/>
Employee employee = (Employee) applicationContext.getBean(“emp1”);--> Invalid
Employee employee = (Employee) applicationContext.getBean(“emp1emp2emp3”);----> Valid
<bean id=”emp1 emp2 emp3” class=”com.durgasoft.beans.Employee”>
Employee employee = (Employee) applicationContext.getBean(“emp1”);---> Invalid
Employee employee = (Employee) applicationContext.getBean(“emp2”);---> Invalid
Employee employee = (Employee) applicationContext.getBean(“emp1 emp2 emp3”); --->valid
<bean id=”emp1,emp2,emp3” class=”com.durgasoft.beans.Employee”>
Employee employee = (Employee) applicationContext.getBean(“emp1”);---> Invalid
Employee employee = (Employee) applicationContext.getBean(“emp2”);---> Invalid
Employee employee = (Employee) applicationContext.getBean(“emp1,emp2,emp3”); --->valid
<bean id=”emp1;emp2;emp3” class=”com.durgasoft.beans.Employee”>
Employee employee = (Employee) applicationContext.getBean(“emp1”);---> Invalid
Employee employee = (Employee) applicationContext.getBean(“emp2”);---> Invalid
Employee employee = (Employee) applicationContext.getBean(“emp1;emp2;emp3”); --->valid
<bean name=”emp1” class=”com.durgasoft.beans.Employee”/>
Employee employee = (Employee)applCntx.getBean(“emp1”); → Valid
<bean name=”emp1 emp2 emp3” class=”com.durgasoft.beans.Employee”/>
Employee e1 = (Employee)applCntx.getBean(“emp1”); → Valid
Employee e2 = (Employee)applCntx.getBean(“emp2”); → Valid
Employee e3 = (Employee)applCntx.getBean(“emp3”); → Valid
Employee emp = (Employee)applCntx.getBean(“emp1 emp2 emp3”);→Invalid
<bean name=”emp1,emp2,emp3” class=”com.durgasoft.beans.Employee”/>
Employee e1 = (Employee)applCntx.getBean(“emp1”); → Valid
Employee e2 = (Employee)applCntx.getBean(“emp2”); → Valid
Employee e3 = (Employee)applCntx.getBean(“emp3”); → Valid
Employee emp = (Employee)applCntx.getBean(“emp1,emp2,emp3”);→Invalid
<bean name=”emp1;emp2;emp3” class=”com.durgasoft.beans.Employee”/>
Employee e1 = (Employee)applCntx.getBean(“emp1”); → Valid
Employee e2 = (Employee)applCntx.getBean(“emp2”); → Valid
Employee e3 = (Employee)applCntx.getBean(“emp3”); → Valid
Employee emp = (Employee)applCntx.getBean(“emp1;emp2;emp3”);→Invalid
In the Spring configuration file we are able to define alias names to the bean components explicitly by using the <alias> tag.
<alias name=”--” alias=”--”/>
Where the “name” attribute will take the original bean identity. Where the “alias” attribute will take the duplicate name[alias name] to the bean.
Example project
Employee.java
package com.durgasoft.beans;
public class Employee {
private int eno;
private String ename;
private float esal;
private String eaddr;
public int getEno() {
return eno;
}
public void setEno(int eno) {
this.eno = eno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public float getEsal() {
return esal;
}
public void setEsal(float esal) {
this.esal = esal;
}
public String getEaddr() {
return eaddr;
}
public void setEaddr(String eaddr) {
this.eaddr = eaddr;
}
@Override
public String toString() {
return "Employee{" +
"eno=" + eno +
", ename='" + ename + '\'' +
", esal=" + esal +
", eaddr='" + eaddr + '\'' +
'}';
}
}
Spring-Config.xml
<?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.xsd">
<bean name="emp1" class="com.durgasoft.beans.Employee">
<property name="eno" value="111"/>
<property name="ename" value="Durga"/>
<property name="esal" value="50000"/>
<property name="eaddr" value="Hyd"/>
</bean>
<alias name="emp1" alias="emp2"/>
<alias name="emp2" alias="emp3"/>
</beans>
Main.java
import com.durgasoft.beans.Employee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring-Config.xml");
Employee employee1 = (Employee) applicationContext.getBean("emp1");
System.out.println(employee1);
Employee employee2 = (Employee) applicationContext.getBean("emp2");
System.out.println(employee2);
Employee employee3 = (Employee) applicationContext.getBean("emp3");
System.out.println(employee3);
}
}
Employee{eno=111, ename='Durga', esal=50000.0, eaddr='Hyd'}
Employee{eno=111, ename='Durga', esal=50000.0, eaddr='Hyd'}
Employee{eno=111, ename='Durga', esal=50000.0, eaddr='Hyd'}
Bean Scopes in the Spring Framework
In Spring applications, to create the number of bean objects we will use scopes.
For the bean components, Spring Framework has provided the following scopes.
- singleton
- prototype
- request
- application
- session
- globalSession
- websocket
From the above list of Scopes , only Singleton and prototype scopes are supported by the Spring Core Module and all the remaining scopes are supported by the Spring WEB MVC module.
Note: In the Spring Applications, the default scope for all the bean components is “Singleton” scope.
singleton
This scope allows the IOC Container to create a single bean object for each and every bean definition in the Spring configuration file.
Hello.java
package com.durgasoft.beans;
public class Hello {
public Hello(){
System.out.println("Hello class Instantiation");
}
public String sayHello(){
return "Hello user!";
}
}
Spring-Config.xml
<?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.xsd">
<bean name="hello1" class="com.durgasoft.beans.Hello" scope="singleton"/>
<bean name="hello2" class="com.durgasoft.beans.Hello" scope="singleton"/>
</beans>
Main.java
import com.durgasoft.beans.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring-Config.xml");
Hello hello1 = (Hello) applicationContext.getBean("hello1");
System.out.println(hello1);
Hello hello2 = (Hello) applicationContext.getBean("hello1");
System.out.println(hello2);
Hello hello3 = (Hello) applicationContext.getBean("hello1");
System.out.println(hello3);
System.out.println();
Hello hello4 = (Hello) applicationContext.getBean("hello2");
System.out.println(hello4);
Hello hello5 = (Hello) applicationContext.getBean("hello2");
System.out.println(hello5);
Hello hello6 = (Hello) applicationContext.getBean("hello2");
System.out.println(hello6);
}
}
Hello class Instantiation
Hello class Instantiation
com.durgasoft.beans.Hello@314c508a
com.durgasoft.beans.Hello@314c508a
com.durgasoft.beans.Hello@314c508a
com.durgasoft.beans.Hello@10b48321
com.durgasoft.beans.Hello@10b48321
com.durgasoft.beans.Hello@10b48321
Prototype Scope:
In the Spring applications, prototype scope allows the IOC Container to create a separate bean object for every request of the bean object through the getBean() method.
Note: It is not a default scope for the beans in Spring applications.
Hello.java
package com.durgasoft.beans;
public class Hello {
public Hello(){
System.out.println("Hello class Instantiation");
}
public String sayHello(){
return "Hello user!";
}
}
Spring-Config.xml
<?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.xsd">
<bean name="hello1" class="com.durgasoft.beans.Hello" scope="prototype"/>
<bean name="hello2" class="com.durgasoft.beans.Hello" scope="prototype"/>
</beans>
Main.java
import com.durgasoft.beans.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring-Config.xml");
Hello hello1 = (Hello) applicationContext.getBean("hello1");
System.out.println(hello1);
Hello hello2 = (Hello) applicationContext.getBean("hello1");
System.out.println(hello2);
Hello hello3 = (Hello) applicationContext.getBean("hello1");
System.out.println(hello3);
System.out.println();
Hello hello4 = (Hello) applicationContext.getBean("hello2");
System.out.println(hello4);
Hello hello5 = (Hello) applicationContext.getBean("hello2");
System.out.println(hello5);
Hello hello6 = (Hello) applicationContext.getBean("hello2");
System.out.println(hello6);
}
}
Hello class Instantiation
com.durgasoft.beans.Hello@6a79c292
Hello class Instantiation
com.durgasoft.beans.Hello@37574691
Hello class Instantiation
com.durgasoft.beans.Hello@25359ed8
Hello class Instantiation
com.durgasoft.beans.Hello@21a947fe
Hello class Instantiation
com.durgasoft.beans.Hello@5606c0b
Hello class Instantiation
com.durgasoft.beans.Hello@80ec1f8
Note: In the Spring application, if we provide “prototype” scope to the bean component then the ApplicationContext container will create a bean object after accessing the getBean() method only, not at its startup time.
Request Scope
In Spring Web MVC applications, Request Scope allows the IOC Container to create a separate bean object for each and every request submitted by the user.
Application Scope
In Spring Web MVC applications, Application Scope allows the IOC Container to create a separate Bean object for each and every ServletContext object.
Session Scope
In Spring Web MVC applications, Session Scope allows the IOC Container to create a separate Bean object for each and every User Session.
globalSession Scope
In Spring Web MVC applications, globalSession scope allows the IOC Container to create a separate Bean object for each and every Portletcontext.
WebSocket Scope
In the Spring Web MVC applications, WebSocket scope allows the IOC Container to create a separate bean object for each and every websocket lifecycle.