×
DocsConsole

DocsConsole

Spring Beans Overview

By | | Updated : 2018-01-01 | Viewed : 562 times

Spring Beans Overview

The article explains about the Spring bean and its creation process. Here the current article describes What a Spring bean is? How Spring Container creates the Bean? How to configure the Spring Bean in bean.xml?

Spring Bean Introduction

Spring bean is a normal POJO (Plain Old Java Object) class, which is managed by Spring Container. Spring container manage the Spring Bean life cycle, i.e., from instantiation to destruction of the bean. Spring Container creates Spring bean by fetching the bean's meta-data in the beans.xml. Here Spring container use the concept of Dependency Injection to handle dependent bean injection into the dependency bean. Inversion Of Control used by Spring Container, to create the Bean object.

IOC and Dependency Injection

IOC and Dependency Injection are main concepts for Bean instantiation and initialization in Spring.

Inversion of Control

This is a process which will be used for the creation of Spring Bean instances. Here Spring Bean means regular Java class. Instead of object creation manually, the Spring Container creates the object. Spring container needs metadata for Spring Beans, i.e., beans.xml to object creation. In beans.xml file contains the beans definition.

Dependency Injection

Dependency Injection defines how dependency bean should be injected into the dependent Bean. There Three types of Dependency Injections are available. Those are 1. Constructor Injection 2. Setter Injection 3. Interface Injection. Spring framework uses only Constructor Injection and Setter Injection. Please verify Service-Locator-Pattern about the detailed explanation of IOC and Dependency injection.

Spring Bean Configuration

  • XML Based Configuration

  • Annotation Based Configuration

  • Java Based Configuration

XML Based Configuration

beans.xml
<bean id="aSpringBean" class="com.docsconsole.tutorials.spring5.core.ASpringBean" >
	<property name="beanName" value="aTestedSpringBean"></property>
</bean>

In the above the beans.xml, we configured ASpringBean bean class within the bean tag element. Here we have provided the id and class properties. These properties should have unique values in beans.xml

Annotation Based Configuration

BSpringBean.java
public class BSpringBean {
	
	private ASpringBean aSpringBean;
	
	//getter for aSpringBean
	
	@Autowired
	public void setaSpringBean(ASpringBean aSpringBean) {
		this.aSpringBean = aSpringBean;
	}
}

Here BSpringBean has dependency as ASpringBean. In the Setter method of ASpringBean, the @Autowired is annotated. Spring Container fetch ASpringBean’s metadata form beans2.xml to create the ASpringBean instance.

beans2.xml
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       	 https://www.springframework.org/schema/beans/spring-beans.xsd
       	 http://www.springframework.org/schema/context
       	 https://www.springframework.org/schema/context/spring-context.xsd">

	<context:annotation-config />

	<bean id="aSpringBean" class="com.docsconsole.tutorials.spring5core.example3.ASpringBean" />
	<bean id="bSpringBean" class="com.docsconsole.tutorials.spring5core.example3.BSpringBean">
	</bean>

</beans> 

Java Based Configuration

AppConfig.java
@Configuration
public class AppConfig {

	@Bean
	public ASpringBean getASpringBean() {
		ASpringBean aSpringBean = new ASpringBean();
		aSpringBean.setBeanName("aTestedSpringBean");
		return aSpringBean;
	}
}

Here we annotated AppConfig Class with @Configuration annotation. AppConfig Class has methods which are annotated with @Bean. So, Spring container process these methods to generate the Spring Bean Object.

Leave A Reply