Spring Boot @Configuration Example

1. Introduction

The @Configuration annotation in Spring Boot is a core component of the framework's Java-based configuration mechanism. This annotation indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.

Key Points:

1. @Configuration classes are the source of bean definitions.

2. They replace XML-based configuration, providing a type-safe way to configure the application context.

3. @Configuration classes can include @Bean-annotated methods, @Import annotations to include other configuration classes, and more advanced techniques such as defining @Profile-specific configurations.

2. Implementation Steps

1. Create a Spring Boot application.

2. Add a @Configuration class to define beans.

3. Write @Bean methods inside the @Configuration class to instantiate and configure dependencies.

4. Inject the beans managed by the Spring container into components using @Autowired.

5. Run the application and utilize the configured beans.

3. Implementation Example

// Step 2: Add a Configuration class
@Configuration
public class AppConfig {

    // Step 3: Define a bean method for a service class
    @Bean
    public MyService myService() {
        return new MyService();
    }
}

// A simple service class
public class MyService {
    public String serve() {
        return "Service is operating";
    }
}

// A component that uses the defined service
@Component
public class MyComponent {

    private final MyService myService;

    // Step 4: Inject the service into the component using @Autowired
    @Autowired
    public MyComponent(MyService myService) {
        this.myService = myService;
    }

    public void executeService() {
        System.out.println(myService.serve());
    }
}

// Main class
@SpringBootApplication
public class SpringBootConfigurationExampleApplication {

    public static void main(String[] args) {
        // Step 5: Run the application
        ApplicationContext context = SpringApplication.run(SpringBootConfigurationExampleApplication.class, args);
        MyComponent myComponent = context.getBean(MyComponent.class);
        myComponent.executeService();
    }
}

// To execute, run the main method or use your IDE's run functionality.

Output:

Service is operating

Explanation:

1. AppConfig is annotated with @Configuration, signaling to Spring that it contains bean definitions.

2. Within AppConfig, a method annotated with @Bean named myService() defines how to instantiate MyService.

3. MyComponent is a typical Spring component that requires an instance of MyService. It uses constructor injection to get an instance of MyService, which is managed by Spring's IoC container.

4. The main class, SpringBootConfigurationExampleApplication, boots the Spring application. It retrieves MyComponent from the application context and calls executeService() on it.

5. The output is a print statement from MyComponent.executeService(), which confirms that MyService was injected successfully and its serve() method was called.


Comments