Spring Boot @Bean Example

1. Introduction

The @Bean annotation in Spring Boot is a crucial mechanism for bean creation. It allows developers to create beans manually, especially when you can't use stereotype annotations like @Component. This is essential for integrating third-party libraries or customizing bean instantiation logic.

Key Points:

1. @Bean is a method-level annotation that indicates a bean should be produced by the Spring container.

2. It is typically used in @Configuration annotated classes to define beans with custom creation logic.

3. Beans defined with @Bean are managed by Spring, meaning the Spring framework will handle their life cycle.

2. Implementation Steps

1. Create a Java class annotated with @Configuration.

2. In the configuration class, define a method with @Bean to instantiate the bean.

3. Inject the bean into components using @Autowired.

4. Run the application to test the configuration.

3. Implementation Example

Here is the complete code that demonstrates the usage of @Bean annotation:
// Define a configuration class to hold bean definitions
@Configuration
public class AppConfig {

    // Define a bean creation method with @Bean
    @Bean
    public TransferService transferService() {
        return new TransferServiceImpl();
    }
}

// Create a service interface
public interface TransferService {
    String transfer();
}

// Create an implementation of the service interface
class TransferServiceImpl implements TransferService {
    public String transfer() {
        return "Transfer completed.";
    }
}

// Create a component that uses the TransferService
@Component
public class TransferController {

    private final TransferService transferService;

    // Autowire the TransferService bean into the component
    @Autowired
    public TransferController(TransferService transferService) {
        this.transferService = transferService;
    }

    public void initiateTransfer() {
        System.out.println(transferService.transfer());
    }
}

// Main application class to bootstrap the application
@SpringBootApplication
public class BeanAnnotationApp implements CommandLineRunner {

    private final TransferController transferController;

    // Constructor injection of TransferController
    @Autowired
    public BeanAnnotationApp(TransferController transferController) {
        this.transferController = transferController;
    }

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

    @Override
    public void run(String... args) {
        // Call the initiateTransfer method after the application context is loaded
        transferController.initiateTransfer();
    }
}

Output:

Transfer completed.

Explanation:

1. @Configuration in AppConfig indicates that the class defines bean methods.

2. @Bean on the transferService method signals Spring to invoke this method and register the return value as a bean within the Spring application context.

3. TransferServiceImpl is a concrete implementation of TransferService that will be managed by Spring.

4. TransferController is a Spring-managed component that depends on the TransferService. It uses @Autowired to have Spring inject the service.

5. BeanAnnotationApp is marked with @SpringBootApplication and implements CommandLineRunner to execute the initiateTransfer method once the Spring context is ready.

6. The output "Transfer completed." is printed to the console, confirming that the TransferService bean has been correctly injected and used by TransferController.


Comments