0 day streak
java / beginner
Snippet

Creating Beans with @Configuration

A class annotated with @Configuration is a source of bean definitions. Methods annotated with @Bean return objects that Spring will register as beans in the application context.

snippet.java
java
1
2
3
4
5
6
7
8
9
10
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public String appVersion() {
return "1.0.0-PROTOTYPE";
}
}
spring
Breakdown
1
@Configuration
Tells Spring that this class contains bean setup logic.
2
@Bean
Declares that the method result should be managed as a bean.