0
0
SpringbootHow-ToBeginner · 4 min read

How to Create Bean in Spring Boot: Simple Guide

In Spring Boot, you create a bean by defining a class annotated with @Component or by declaring a method annotated with @Bean inside a configuration class. Spring Boot automatically detects these beans and manages their lifecycle in the application context.
📐

Syntax

There are two main ways to create a bean in Spring Boot:

  • @Component annotation: Place this on a class to let Spring detect and register it automatically.
  • @Bean annotation: Use this on a method inside a @Configuration class to explicitly define a bean.

Spring Boot scans for these annotations and creates beans accordingly.

java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {
    // Bean created by component scanning
}

@Configuration
public class MyConfig {
    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

public class MyBean {
    // Bean created by @Bean method
}
💻

Example

This example shows a Spring Boot application creating beans using both @Component and @Bean. The application prints messages from both beans to demonstrate they are managed by Spring.

java
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
public class BeanExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(BeanExampleApplication.class, args);
    }

    @Bean
    public CommandLineRunner runner(MyComponent myComponent, MyBean myBean) {
        return args -> {
            myComponent.sayHello();
            myBean.sayHello();
        };
    }
}

@Component
class MyComponent {
    public void sayHello() {
        System.out.println("Hello from MyComponent bean!");
    }
}

@Configuration
class MyConfig {
    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

class MyBean {
    public void sayHello() {
        System.out.println("Hello from MyBean created by @Bean!");
    }
}
Output
Hello from MyComponent bean! Hello from MyBean created by @Bean!
⚠️

Common Pitfalls

Common mistakes when creating beans in Spring Boot include:

  • Forgetting to annotate the class with @Component or the method with @Bean, so Spring does not detect the bean.
  • Not placing @Bean methods inside a class annotated with @Configuration.
  • Creating multiple beans of the same type without specifying qualifiers, causing ambiguity.
  • Using new to instantiate beans manually instead of letting Spring manage them.
java
/* Wrong: Missing @Component annotation, bean won't be created */
public class MissingComponent {
}

/* Right: Add @Component to register bean */
@Component
public class CorrectComponent {
}

/* Wrong: @Bean method outside @Configuration class */
public class WrongConfig {
    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

/* Right: @Bean method inside @Configuration class */
@Configuration
public class CorrectConfig {
    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}
📊

Quick Reference

Summary tips for creating beans in Spring Boot:

  • Use @Component on classes for automatic detection.
  • Use @Bean on methods inside @Configuration classes for explicit bean creation.
  • Ensure your package is scanned by Spring Boot (usually by placing classes under the main application package).
  • Use constructor injection to get beans instead of manual instantiation.

Key Takeaways

Annotate classes with @Component or methods with @Bean inside @Configuration to create beans.
Spring Boot auto-detects beans in scanned packages and manages their lifecycle.
Always place @Bean methods inside @Configuration classes for proper registration.
Avoid manual instantiation; let Spring inject beans to benefit from dependency management.
Use qualifiers if multiple beans of the same type exist to avoid ambiguity.