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
One comment on “Methods of Bean Configurations for Spring Container
  1. Jean Bell says:

    Hi Bru,

    Nice to be visiting your blog again, it has been months for me. Well this article that i’ve been waited for so long.

    I want to consme the web which is created by SAP PI (its middleware tool), I got the WSDL file, Next i created one java project in
    Eclipse and imported the wsdl
    file in to the project and right click on the file and choose option Generate client. Now 6 java files have cretaed , next i dont know what i want to do kindly help me on this. any jar file i want to import or what all are the steps required to send the data from my java application.

    It’s standalone java appliation,

    Once again thanks for your tutorial.

    Thank you,

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

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

Join 679 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
  • 324,998 hits