Spring Boot Java Based Configuration

1. Introduction

Spring Boot simplifies the process of working with Spring Framework by removing much of the boilerplate code and providing out-of-the-box configurations. One of the key features of Spring Boot is its ability to support Java-based configuration, which allows developers to configure their applications programmatically with annotations. This approach is not only cleaner but also type-safe and easy to refactor. 

We configure Java-based configuration using @Configuration and @Bean annotations in Spring-based applications. In this blog post, we will discuss how to create a simple Spring Boot application with Java-based configuration and how to invoke methods at startup to demonstrate the concepts.

2. Implementation Steps

1. Initialize a new Spring Boot project with the necessary dependencies.

2. Create a configuration class annotated with @Configuration that defines bean methods.

3. Implement a service class with the logic you wish to execute at startup.

4. Develop the main Spring Boot application class with @SpringBootApplication.

5. Utilize the CommandLineRunner interface to invoke methods after the application context is loaded.

3. Implementation Example

// Step 1: Configuration class to define beans
@Configuration
public class AppConfig {

    // Step 2: Define a bean to return a service instance
    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}

// Step 3: Service class implementation
public interface MyService {
    void performAction();
}

@Service
public class MyServiceImpl implements MyService {

    @Override
    public void performAction() {
        // Implementation of the action
        System.out.println("Action performed!");
    }
}

// Step 4: Main application class
@SpringBootApplication
public class MySpringBootApplication implements CommandLineRunner {

    // Step 5: Inject the service class via constructor
    private final MyService myService;

    public MySpringBootApplication(MyService myService) {
        this.myService = myService;
    }

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

    @Override
    public void run(String... args) {
        // Step 6: Call the service method to perform the action
        myService.performAction();
    }
}

Output:

Upon starting the Spring Boot application, you should see "Action performed!" printed to the console, indicating that the service method has been successfully invoked.

Explanation:

1. @Configuration: Classes annotated with this are considered as configuration classes by Spring.

2. @Bean: Methods annotated with this will return objects that should be registered as beans in the Spring application context.

3. MyService: Interface for the service layer; defines performAction method.

4. MyServiceImpl: Implementation of MyService interface; contains the business logic.

5. @SpringBootApplication: Entry point for the Spring Boot application; also a configuration annotation.

6. CommandLineRunner: An interface used to indicate that a bean should run when it is contained within a SpringApplication.

7. @Override: Method overriding, in this case, run method from CommandLineRunner interface.

8. System.out.println: Used to print the output to the console.

9. SpringApplication.run(): Launches the application.

10. Constructor Injection: MyService is injected into MySpringBootApplication through constructor injection, a recommended approach for dependency injection in Spring.


Comments