Methods of Bean Configurations for Spring Container

There are three ways to provide the configuration metadata to Spring container. Below are the more details about them.

1. Xml based configuration file

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”;

<!– A simple bean definition –>
<bean id=”…” class=”…”>
<!– collaborators and configuration for this bean go here –>

<!– A bean definition with lazy init set on –>
<bean id=”…” class=”…” lazy-init=”true”>
<!– collaborators and configuration for this bean go here –>

<!– A bean definition with initialization method –>
<bean id=”…” class=”…” init-method=”…”>
<!– collaborators and configuration for this bean go here –>

<!– A bean definition with destruction method –>
<bean id=”…” class=”…” destroy-method=”…”>
<!– collaborators and configuration for this bean go here –>

<!– more bean definitions go here –>


2. Annotation based configuration

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”;

<!– bean definitions go here –>

<bean id=”empAddress” class=”in.malliktalksjava.spring.samples.EmployeeAddress” />


Once <context:annotation-config/> is configured, you can start annotating your code to indicate that Spring should automatically wire values into properties, methods, and constructors. Once you add the annotation config, you can use @Autowired annotation as below on setter methods to get rid of the <property> element in XML configuration file. When Spring finds an @Autowired annotation used with setter methods, it tries to perform byType autowiring on the method.

package in.malliktalksjava.spring.samples;

public class Employee{


public EmployeeAddress empAddress;

public void test(){





package in.malliktalksjava.spring.samples;

public class EmployeeAddress{

private String street;

private String postalCode;

public void setStreet(String street){

this.street = street;


public String getStreet(){

return street;


public void setPostalCode(String postalCode)

this.postalCode = postalCode;


public String getPostalCode(){

return postalCode;




3. Java based configuration

Java based configuration option enables you to write most of your Spring configuration without XML but with the help of few Java-based annotations. Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.

package in.malliktalksjava.spring.samples;
import org.springframework.context.annotation.*;

public class HelloWorldConfig {

public HelloWorld helloWorld(){
return new HelloWorld();

package in.malliktalksjava.spring.samples;

public class HelloWorld {
private String message;

public void setMessage(String message){
this.message = message;

public void getMessage(){
System.out.println(” Message is : ” + message);

package in.malliktalksjava.spring.samples;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;

public class MainApp {
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(HelloWorldConfig.class);

HelloWorld helloWorld = ctx.getBean(HelloWorld.class);

helloWorld.setMessage(“Hello World!”);

Complete application has been created without writing any configuration file.


I born in Good cultural place called India. My family is traditional family. We fallow all Indian traditions.

Tagged with: , , ,
Posted in Spring

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 677 other followers

June 2015
« May   Aug »
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 237,369 hits
All tags
AngularJS Apache Cassandra Arrays in Java Cassandra Cassandra Database Cassandra examples client stubs creating a java web service creating a web service using java difference between interface and abstract class Differences between SOAP and RESTfull webservices Display div as Overlay using JQuery download the spring basic application File directory Generating webservice client stubs Hibernate hibernate exceptions How to display over using JQuery how to generated web service client stubs in STS How to implement Spring basic application how to write singleton design pattern in java interface in java Java Java Arrays Java Example Programs Java examples Java interview questions java programs java project Java sample Programs Java Script JavaScript JavaServer Faces Java Sorting alogorithms Java Tutorials javatutorials Java Web services JDBC open shift openshift Overlay implementation using JQuery Overlay in JQuery POI API POI Examples public class public static void main Redhat OpenShift representational state transfer Restfull webservices sample programs in java Service-oriented architecture Singleton design pattern in java SOAP SOAP Webservices software Spring Framework Spring Interview Questions Spring MVC Spring MVC Example program Spring MVC sample Program Spring Sample Programs Spring Tutorials Struts technology Top Java Interview questions w3c dom Web service web service client Web Service Description Language webservices Web services writing singleton design pattern in Java WSDL Xml xml parsers