Tuesday, August 12, 2014

Beans in Spring

In Spring everything is actually about Beans. And what is a bean. A bean is an object which is managed by Spring container or factory. The container and factory are used interchangeably. You can think it of a runtime container which maintains the handle to all the objects created by bean.

Let's make a simple POJO class and ask spring to create the object for us. Make a Java project in your IDE and create a POJO class.

User.java

public class User {

  private String firstName;
  private String lastName;

  public String getFirstName() {
   return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
   return lastName;
 }

 public void setLastName(String lastName) {
   this.lastName = lastName;
 }
}

Now write the context.xml and put it in classpath.

<?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-2.5.xsd">

    <bean id="user" class="User" />

</beans>

Write the main method

String[] files = {"Context.xml"};
ApplicationContext appContext = new ClassPathXmlApplicationContext(files);

//Get the object from Spring container
User user = (User) appContext.getBean("user");

Beans Naming

For beans naming use Standard Java Convention of naming Fields. The name of the bean must be unique in the Bean Factory. It is not allowed for two beans to have same name as this brings confusion for the factory. To figure out the name of bean, if the <bean> tag has an id attribute, the value of the id attribute is used as the name. If there is no id attribute, Spring looks for name attribute. Multiple names can be given with comma separation. If neither id nor name attribute are defined, Spring use the class name as the name.

Bean having name as user

<bean id=”user” class=”User”/>

If there is no id but a name, so bean name is user

<bean name=”user” class=”User”/>

Bean name is the class name - User

<bean class=”User”/>

This bean can be accessed by using user, userA or userB

<bean id=” user” name=”userA,userB” class=”User”/>

Bean names can be gives aliases also

<alias name="fromName" alias="toName"/>

Only one id can be specified but multiple names can be given which essentially become an alias.

Bean Scopes

Spring beans can exit in different scopes. The scope that are allowed in Spring are:
  • singleton- Only one instance of object is created. It's like singleton but in the scope of Spring container.
  • prototype - Every time the beans is requested from the factory, a new instance is created and returned.
  • request - Valid in web application. The scope is in the request scope.
  • session - Valid in web application. The scope is in the session scope.
  • global-session - valid in portlet context.

By default all the beans in spring are singleton. The notion of singleton is different from what we understand the notion as in singleton pattern. In Singleton notion only one object per class is created in the context of the class loader. The notion of singleton as employed in Spring is limited to the spring container against that name of the bean. It's possible to create another object of same class employing a different bean. It's possible to create the object of the class by invoking the constructor yourself. Also it is possible to have another object instance in a different spring container.

The prototype scope is mentioned while defining the bean in configuration XML.

<bean id="user" class="User" scope="prototype">

For prototype beans spring only manages the creation lifecycle. Spring does not manages the bean once it hands it over, which means no destruction life cycle happens on bean.also be careful that if a prototype bean is referenced by a singleton bean, than calling a getter method on singleton bean for prototype bean will not return a new instance. The prototype bean dependency is resolved when the singleton bean is created.

More Articles on Spring

No comments:

Post a Comment