What is @Configuration Annotation in Spring?

Anh Trần Tuấn - Sep 12 - - Dev Community

1. Understanding @Configuration Annotation

The @Configuration annotation in Spring is used to indicate that a class declares one or more @bean methods. These @bean methods are then processed by the Spring container to generate bean definitions and service requests for those beans at runtime.

1.1 How @Configuration Works

When you annotate a class with @Configuration , Spring treats it as a source of bean definitions. This means that Spring will scan the class for methods annotated with @bean and register those methods' return types as beans in the Spring application context.

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, AppConfig is a configuration class, and myService is a bean that Spring will manage. The myService bean will be available for dependency injection wherever it's needed.

1.2 Importance of @Configuration

Using @Configuration helps to organize your Spring application into manageable components. By keeping the bean definitions in dedicated configuration classes, you achieve a higher level of modularity, making your code easier to maintain and scale.

1.3 The Role of @bean Methods

The methods inside a @Configuration class that are annotated with @bean are critical in defining the objects that will be managed by the Spring IoC container. Each method returns an instance of the bean, and Spring ensures that this instance is a singleton by default.

1.4 Demo: A Simple Spring Application with @Configuration

Let's create a simple Spring application to demonstrate how the @Configuration annotation works.

Maven Dependencies

First, add the necessary dependencies to your pom.xml :

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.21</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Configuration Class

Create a configuration class named AppConfig :

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}
Enter fullscreen mode Exit fullscreen mode

Service Class

Next, create a service interface and its implementation:

public interface MyService {
    String sayHello();
}

public class MyServiceImpl implements MyService {

    @Override
    public String sayHello() {
        return "Hello, Spring!";
    }
}
Enter fullscreen mode Exit fullscreen mode

Main Application

Finally, create the main application class:

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

public class MainApp {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        MyService myService = context.getBean(MyService.class);
        System.out.println(myService.sayHello());
    }
}
Enter fullscreen mode Exit fullscreen mode

Running the Application

When you run the MainApp class, the output will be:

Hello, Spring!
Enter fullscreen mode Exit fullscreen mode

This output confirms that the myService bean was successfully created and managed by Spring.

2. Advanced Concepts with @Configuration

In this section, we'll explore some advanced concepts related to the @Configuration annotation.

2.1 Full vs. Lite Mode in @Configuration

The @Configuration annotation can operate in two modes: Full and Lite. Full mode is the default and ensures that @bean methods are enhanced to support the full lifecycle of beans. In Lite mode, the class is not considered as a full @Configuration , which means @bean methods are treated as plain factory methods without full container management.

@Configuration(proxyBeanMethods = false)
public class AppConfigLite {

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}
Enter fullscreen mode Exit fullscreen mode

In Lite mode (In Lite mode (proxyBeanMethods = In Lite mode (proxyBeanMethods = false), each call to a @bean method will result in the creation of a new instance.), each call to a @bean method will result in the creation of a new instance.= false), each call to a @bean method will result in the creation of a new instance.

2.2 Interaction with Other Annotations

The @Configuration annotation often works alongside other Spring annotations like @ComponentScan , @PropertySource , and @Import. These annotations allow for more comprehensive configuration setups in your Spring applications.

@Configuration
@ComponentScan(basePackages = "com.example.services")
@PropertySource("classpath:application.properties")
public class AppConfig {

    // bean definitions
}
Enter fullscreen mode Exit fullscreen mode

This example demonstrates a more sophisticated configuration class that scans for components in a specific package and loads properties from an external file.

3. Conclusion

The @Configuration annotation is a foundational element in Spring that enables developers to organize and manage bean definitions effectively. It’s a key part of building modular, maintainable Spring applications. Whether you're just starting with Spring or you're an experienced developer, understanding and using @Configuration effectively can significantly enhance your application design.

If you have any questions or want to discuss this further, feel free to comment below!

Read posts more at : What is @Configuration Annotation in Spring?

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .