Spring Bean Definition by R4R Team

Spring Bean Definition:-
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 otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container, The bean definition contains the information called configuration metadata which is needed for the container to know the followings:
1 How to create a bean
2 Bean's lifecycle details
3 Bean's dependencies

All the above configuration metadata translates into a set of the following properties that make up each bean definition.




This attribute is mandatory and specify the bean class to be used to create the bean.


This attribute specifies the bean identifier uniquely. In XML-based configuration metadata, you use the id and/or name attributes to specify the bean identifier(s).


This attribute specifies the scope of the objects created from a particular bean definition.


This is used to inject the dependencies


This is used to inject the dependencies.

autowiring mode

This is used to inject the dependencie.

lazy-initialization mode

A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.

initialization method

A callback to be called just after all necessary properties on the bean have been set by the container.

destruction method

A callback to be used when the container containing the bean is destroyed

Spring Configuration Metadata:-
Spring IoC container is totally decoupled from the format in which this configuration metadata is actually written. There are following three important methods to provide configuration metadata to the Spring Container:

1 XML based configuration file
2 Annotation-based configuration
3 Java-based configuration

1 XML based configuration file:-The XML Schema-based configuration introduced in Spring 2.0 and enhanced and extended in Spring 2.5 and 3.0.
The central motivation for moving to XML Schema based configuration files was to make Spring XML configuration easier. The 'classic' <bean/>-based approach is good, but its generic-nature comes with a price in terms of configuration overhead.

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

<bean id="..." class="..."></bean>

<bean id="..." class="..." lazy-init="true"></bean>

<bean id="..." class="..." init-method="..."></bean>

<bean id="..." class="..." destroy-method="..."></bean>


2 Annotation-based configuration:- Spring 2.5 introduced support for annotation-based configuration metadata.Starting from Spring 2.5 it became possible to configure the dependency injection using annotations. So instead of using XML to describe a bean wiring, you can move the bean configuration into the component class itself by using annotations on the relevant class, method, or field declaration.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"







<!-- bean definitions here -->


Some important annotations are as follows:-

1 @Required The @Required annotation applies to bean property setter methods.
2 @Autowired The @Autowired annotation can apply to bean property setter methods, non-setter methods, constructor and properties.
3 @Qualifier The @Qualifier annotation along with @Autowired can be used to remove the confusion by specifiying which exact bean will be wired.
4 JSR-250 Annotations Spring supports JSR-250 based annotations which include @Resource, @PostConstruct and @PreDestroy annotations.

3 Java-based configuration:- Starting with Spring 3.0, many features provided by the Spring JavaConfig project became part of the core Spring Framework. Thus you can define beans external to your application classes by using Java rather than XML files. To use these new features, see the @Configuration, @Bean, @Import and @DependsOn annotations.

package com.r4r.snippets.enterprise;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import com.r4r.snippets.enterprise.services.HelloWorldService;


public class TestConfig {


public HelloWorldService helloWorldService() {

return new HelloWorldService();


The @Bean annotation over the helloWorld() method indicates that this method produces a bean to be managed by the Spring container.

Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!