Let’s get started with Spring Bean !
Overview
Bean is a key concept of the Spring Framework. Understanding this specific part of Spring Framework is crucial to use it efficiently.
In this post, we will go throught this Spring concept.
What is Spring Bean ?
From the official Spring documentation :
In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.
In Spring, bean is fully part of the IoC container, or “Inversion of Control” :
It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes, or a mechanism such as the Service Locator pattern.
The “Inversion of Control” process allows you to create instances, without creating them using the new Java instruction.
Java sample
In this post, we will use the following simple class :
public class Person { private String firstName; private String lastName; private Address address; public Person() { } public Person(String firstName, String lastName, Address address) { setFirstName(firstName); setLastName(lastName); setAddress(address); } public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return this.lastName; } public void setFirstName(String lastName) { this.lastName = lastName; } public Address getAddress() { return this.address; } public void setAddress(Address address) { this.address= address; } } public class Address { private String street; private int number; public Address() { } public Address(String street, int number) { setStreet(street); setNumber(number); } public String getStreet() { return this.street; } public void setStreet(String street) { this.street= street; } public int getNumber() { return this.number; } public void setNumber(int number) { this.number= number; } }
Java way
In this example, we just want the following user :
John Doe
123 Main St
Anytown, USA
Let’s do this :
Address address = new Address("Main St", 123); Person person = new Person("John", "Doe", address);
As we can see in this approach, we are firstly creating the address instance, and then pass it to the person instance. Is there any alternative to do it ?
Using Spring bean and the xml approach
We will use the following xml :
<beans xmlns="http://springframework.org/schema/beans" xmlns:batch="http://springframework.org/schema/batch" xmlns:task="springframework.org/schema/task" xmlns:util="http://springframework.org/schema/util" xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.2.xsd"> <bean id="address" class="my.package.Address" /> <property name="street" value="Main St" /> <property name="number" value="123" /> </bean> <bean id="person" class="my.package.Person" /> <property name="firstName" value="John" /> <property name="lastName" value="Doe" /> <property name="address" ref="address" /> </bean> </beans>
The Java part :
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Person person = ctx.getBean("person", Person.class);
This xml file will produce, using the “Inversion of Control”, 2 beans. All beans are automatically created using the IoC, and can be retrieved using a getBean call.
Conclusion
Using Spring Bean, all instance declaration is moved from code to specific configuration file (or annotation). All objects are created by the Spring Framework, and can be retrieved independently.