← All tutorials

GoCareerGo Tutorials

Spring Boot Tutorial

Auto-configuration, REST APIs, JPA, AOP, caching, microservices and Spring Cloud.

Spring Beans and @Bean

A bean is any object whose lifecycle (creation, wiring, destruction) is managed by the Spring IoC container.

@Configuration
public class AppConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
  • @Component/@Service/@Repository — beans discovered via classpath scanning
  • @Bean — beans declared explicitly inside a @Configuration class, useful for third-party classes you can't annotate
  • By default beans are singleton-scoped — one instance per container