SPRING CONCEPTS

Spring bean

Spring Concepts / Spring bean

A bean is an object registered with and managed by the Spring container.

Understand the idea

Component scanning finds annotated classes such as @Service and @Controller. @Bean methods register objects you create explicitly, often library objects. The default singleton scope means one instance per application context, shared across requests.

A concrete example

@Bean
PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder(12);
}

In the application

Services and controllers are discovered by scanning; SecurityConfig registers the password encoder and JWT components.

Do not store the current request or current user in singleton controller instance fields.

Detailed walkthrough: How Spring creates and connects objects →

See it in a working stage

Related concepts

Read the official documentation ↗
Java 21 · Spring Boot 4.1.1 · Edupoly