How to Create a Spring Bean in 3 Different Ways

Last Updated : 27 Apr, 2026

Spring Framework is a widely used, open-source Java framework that makes building scalable and reliable enterprise applications easier. It simplifies development compared to traditional Java technologies like JDBC, JSP, and Servlets.

  • Objects in Spring, called beans, are managed by the IoC (Inversion of Control) container.
  • The container handles creation, configuration, and lifecycle management of beans.
  • Enables loose coupling and easier dependency management in applications.

Different Ways to Create a Spring Bean

The following diagram illustrates the different approaches used to create and configure Spring beans.

different_methods_to_create_a_spring_bean

1. Using XML Configuration

XML-Based Configuration is the traditional way of defining Spring beans. In this approach, you create an XML file (commonly beans.xml) where all your beans and their dependencies are declared. The Spring IoC container reads this file at runtime, instantiates the beans, injects dependencies, and manages their lifecycle.

Advantages of XML Configuration

  • Bean configuration is separate from Java code.
  • Developers can change configurations without Disturbing the Java classes.

Step by Steps implementation of XML Configuration

Follow these steps to configure and manage Spring beans using XML-based configuration.

Step 1 : Create the Bean Class

Create a simple Java class that you want to manage by Spring as a bean.

Java
// UserService.java
package com.example;

public class UserService {
    public void showMessage() {
        System.out.println("Hello from UserService!");
    }
}

Step 2: Create the Spring XML Configuration File

Define the bean in an XML file (beans.xml) and provide its class and ID.

XML
<!-- beans.xml -->
<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.xsd">

    <!-- Define UserService bean -->
    <bean id="userService" class="com.example.UserService"/>
</beans>

Step 3: Create Main Class

Load the Spring IoC Container and Get the Bean use ApplicationContext to load the XML configuration and retrieve the bean.

Java
// App.java
package com.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        // Load Spring XML configuration
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        // Retrieve the bean by ID
        UserService userService = (UserService) context.getBean("userService");

        // Call method on the bean
        userService.showMessage();
    }
}

Output:

Hello from UserService!

 2. Using Annotation-Based Configuration

Annotation-Based Configuration allows Spring to automatically detect and manage beans using annotations like @Component and inject dependencies without XML configuration.

Advantages of Annotation-Based Configuration

  • Less boilerplate code
  • Easier to maintain and read
  • Supports modern Spring practices (widely used in Spring Boot)

Step by Steps implementation of Annotation-Based Configuration

Follow these steps to configure and manage Spring beans using Annotation-Based Configuration

Step 1: Create the Bean Class

Use @Component annotation to tell Spring that this class is a bean.

Java
package com.example;

import org.springframework.stereotype.Component;

@Component
public class UserService {
    public void showMessage() {
        System.out.println("Hello from UserService using Annotation-Based Configuration!");
    }
}

Step 2: Create a Configuration Class

Use @Configuration to mark it is a configuration class and @ComponentScan to tell Spring where to look for beans.

Java
// AppConfig.java
package com.example;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
    
    // No @Bean needed here because we are using @Component in UserService
}

Step 3: Create the Main Class to Load Spring Context

Use AnnotationConfigApplicationContext to load the configuration and retrieve the bean.

Java
// App.java
package com.example;

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

public class App {
    public static void main(String[] args) {
        
        // Load Spring context using the configuration class
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        // Retrieve the bean
        UserService userService = context.getBean(UserService.class);

        // Call the bean method
        userService.showMessage();
    }
}

Output:

Hello from UserService using Annotation-Based Configuration!

3. Using Java-Based Configuration

Java-Based Configuration allows you to configure Spring beans using Java classes .You define a configuration class using @Configuration and create beans using the @Bean annotation.

Advantages of Java-Based Configuration

  • All configuration is written in Java, making it cleaner and easier to manage.
  • Configuration and business logic can be clearly structured and organized.
  • Configuration is Done in Java, the compiler checks for errors at compile time.

Step by Steps implementation of Java-Based Configuration

Follow these steps to configure and manage Spring beans using Java-Based Configuration

Step 1: Create Bean Class

Create normal Java class that contain business logic. These classes will be managed by Spring Container.

Java
// UserService.java
package com.example;

public class UserService {
    public void showMessage() {
        System.out.println("Hello from UserService!");
    }
}

Step 2: Create a Configuration Class

In the Spring Framework, a Configuration Class is a Java class used to define and manage Spring beans without using XML.

Java
// AppConfig.java
package com.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public UserService userService() {
        return new UserService();
    }
}

Step 3: Create Main Class.

The Spring IoC container is started by creating an AnnotationConfigApplicationContext and passing the configuration class. The container reads the configuration, creates and initializes all defined beans, and manages them throughout their lifecycle.

Java
// App.java
package com.example;

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

public class App {

    public static void main(String[] args) {

        // Start Spring Container
        ApplicationContext context =
                new AnnotationConfigApplicationContext(AppConfig.class);

        // Get the bean from container
        UserService service = context.getBean(UserService.class);

        // Call method
        service.display();
    }
}

Output:

Hello from UserService!

Comment