You define a bean with Java configuration by writing a class annotated with @Configuration and adding a method annotated with @Bean that returns the object. Spring calls the method once, stores the result in the container under the method name and injects it wherever it is needed.
This replaces the older XML bean definitions. The examples below work with Spring Framework 6 and Spring Boot 3 and later.
A @Bean method is a factory method. The return type is the bean type and the method name is the default bean name.
Here Spring registers a bean named greetingService of type GreetingService. Returning the interface type keeps consumers decoupled from the implementation.
In a plain Java application, create an AnnotationConfigApplicationContext with the configuration class.
In Spring Boot you do not create the context yourself. @SpringBootApplication includes @Configuration, so any @Bean method in that class, or in another @Configuration class that component scanning finds, is registered automatically.
When one bean needs another, declare it as a method parameter. Spring resolves the dependency and passes it in.
Calling greetingService() directly inside another @Bean method also works in a full @Configuration class, because Spring proxies the class and returns the existing singleton. Parameter injection is clearer and also works in lite mode, so prefer it.
The annotation has attributes for the common settings.
| Setting | Effect |
|---|---|
name | Sets the bean name and any aliases |
@Scope("prototype") | Creates a new instance on every request; the default is singleton |
initMethod, destroyMethod | Lifecycle callbacks on the bean |
@Primary | Preferred candidate when several beans match a type |
@Lazy | Creates the bean on first use instead of at startup |
Beans that implement AutoCloseable or have a public close() or shutdown() method are closed automatically by default, so you rarely need destroyMethod.
You can register a bean only in some situations.
In Spring Boot, @ConditionalOnProperty and @ConditionalOnMissingBean give finer control, and are how auto-configuration backs off when you define your own bean.
| Approach | Use it when |
|---|---|
@Bean in @Configuration | You cannot edit the class (third-party library), or construction needs logic |
@Component, @Service, @Repository | You own the class and it can be built by constructor injection |
Mix both: annotate your own classes with stereotypes and use @Bean for libraries such as an ObjectMapper, RestClient or DataSource. Keep @Bean methods public or package-private and not final, since Spring proxies full configuration classes with CGLIB.
The name of the @Bean method. Use the name attribute to change it.
Yes. One shared instance is created per container unless you set another scope such as prototype.
@Component classes can hold @Bean methods in lite mode, but without proxying, so calls between @Bean methods create new objects. Use @Configuration for predictable behaviour.
Yes, and you can import it with @ImportResource, but Java configuration is the modern default.
Annotate a class with @Configuration, return objects from @Bean methods and let Spring manage them as singletons. Pass dependencies as method parameters, use @Scope, @Primary and @Profile to adjust behaviour, and combine this with component scanning for your own classes.