How to create bean in Spring / Various ways to create a bean in Spring / Create a bean in Spring without new keyword

Bean…What is it ?

A bean in java context is a simple java class which has some properties (also called fields) and their getter and setter methods. These properties can be regarded as instance variables of the bean. The name of the properties and their getter/setter methods should adhere to the JavaBeans specifications (should start with a small letter, should be camel-case and so on).

How to create ?

There are many ways to create a bean in Spring and all of them will be discussed here. Well, all of the methods involve creating a class which will be regarded as a bean. So first let’s create a class called User.

  package com.codippa;
 
  public class User {
      private String name;
 
      private String address;
 
      public String getName() {
	return name;
      }
 
      public void setName(String name) {
	this.name = name;
      }
 
      public String getAddress() {
	return address;
      }
 
      public void setAddress(String address) {
	this.address = address;
      }
  }

In a simple java program, we create an instance of this class using new keyword but in Spring, there are different ways to create an instance of a bean class without using new. Although, using new keyword is permitted but is not recommended since injection of properties and Autowiring does NOT work if you create a bean using new.
Let’s discuss the ways to create a bean in Spring.

Method 1 : Declaring a bean in XML configuration file

This is the most primitive approach of creating a bean. The bean is declared in Spring’s XML configuration file. Upon startup, Spring container reads this configuration file and creates and initializes all the beans defined in this file which can be used anytime during application execution. Here is how we define a bean in configuration XML.

<bean id=”user” class=”com.codippa.User”></bean>

Note : This method assumes that you are familiar with Spring XML configuration and how it is configured.

Details

A bean in Spring XML configuration file is defined using element declaration. The id attribute is used to uniquely identify the bean and get its reference during execution (shown later). The class attribute should contain the fully qualified class name. This bean can be retrieved and used in the application in the following way :

public class Main {
 	@SuppressWarnings("resource")
	public static void main(String[] args) {
		ApplicationContext context = new AnnotationConfigApplicationContext(User.class);
		User user = (User)context.getBean("user");
		System.out.println(user.getName());
		System.out.println(user.getAddress());
	}
}

Let’s tweak in :

  1. The id attribute is not mandatory and is only required when you want to access this bean in application or provide this bean as a dependency to some other bean.
  2. The id of a bean should be unique otherwise you will get an error at start up like

    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Bean name ‘test’ is already used in this <beans> element.

  3. The scope of bean shall be singleton by default which means every time the bean is referred, same instance will be returned. The scope of the bean can be changed by introducing a scopeattribute in bean declaration.
  4. Bean properties referred in the propertyelement should have valid setter methods otherwise following error will be received.

    Bean property ‘name’ is not writable or has an invalid setter method.

  5. Spring automatically does the string to number conversion in case the type of a class property is numeric while the values provided in XML configuration file are in string format. But you will get a java.lang.NumberFormatException if the property is not convertible to a number.

Method 2 : Using @Component annotation

@Component annotation above a class indicates that this class is a component and should be automatically detected and instantiated. Thus a Spring component bean will look like :

  package com.codippa;
 
  @Component
  public class User {
      private String name;
 
      private String address;
 
      public String getName() {
	return name;
      }
 
      public void setName(String name) {
	this.name = name;
      }
 
      public String getAddress() {
	return address;
      }
 
      public void setAddress(String address) {
	this.address = address;
      }
  }

Details :

In order for a bean to be auto-discoverable and instantiated, we need to instruct Spring container to find our beans and tell it about the package where it should find the beans. Hence, for instantiating annotated beans, we need to add following declaration in our Spring XML configuration file :

<context:component-scan base-package=”com.codippa” />

If the bean annotated with @Componentannotation is outside of the package given in base-package attribute of the above element, then it will not be scanned and instantiated and thus will not be found. This bean can also be used in the same way as the bean in the first method. Thus, only bean declaration methods differ while the method of using them remains the same.

Let’s tweak in :

  1. @Component annotation can only be applied at class level. Applying it to some other location such as above a field will result in a compiler error The annotation @Component is disallowed for this location.
  2. If a bean is referred and it has not been annotated with @Component or declared in XML configuration then there will be an exception as :

    org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.codippa.User] is defined

  3. Automatically instantiating a bean using @Component annotation requires that there is a default constructor present in the class. A default constructor is a constructor without any arguments. If there is no constructor present in a class then a default constructor is automatically created. If there is a constructor in your bean class which accepts some arguments then you have to create a default constructor yourself. Absence of default constructor will arise an error as

    org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.codippa.User]: No default constructor found

Method 3 : Using @Configuration annotation

Refer How to create bean without XML configuration in Spring to grab details about this method.


Hope this post helped you learn some concept. Great !!! Keep visiting.

This Post Has 4 Comments

  1. Nice Explaition

    1. Thank you. Keep Visiting!!!

  2. Thanks

    1. Keep Visiting!!!

Mark Your Impression

Close Menu