Bean Articles
6 articles about Bean. Explore Spring Boot implementation, design, and operations across related topics.
-
How to Resolve Multiple Beans of the Same Type in Spring Boot - Using @Qualifier and @Primary and Name Resolution Priority
Registering multiple implementations of the same interface, such as payment methods or notification channels, causes a NoUniqueBeanDefinitionException. This article organizes explicit specification with @Qualifier, default specification with @Primary, the priority when both conflict, and fallback resolution by parameter name using practical example code, and implements a configuration where implementations can be switched via application.yml or profiles.
-
When Should You Use BeanNameAware and ApplicationContextAware in Spring Boot?
Explains what BeanNameAware, BeanFactoryAware, ApplicationContextAware, and EnvironmentAware inject into a Bean and when they are called, with implementation examples. Organizes the criteria for deciding when to use them versus when to replace them with constructor injection.
-
Understanding Spring Boot Bean Scopes - When to Use singleton, prototype, request, and session
A thorough guide to the behavior and use cases of Spring Boot's five Bean scopes (singleton/prototype/request/session/application). Covers the pitfall where @Scope("prototype") still returns the same instance, and four workarounds using ObjectProvider, @Lookup, and jakarta.inject.Provider with implementation examples.
-
Understanding Spring Boot Bean Lifecycle - How to Use @PostConstruct, @PreDestroy, and InitializingBean
A visual walkthrough of the Spring Bean lifecycle from creation through initialization to destruction, explaining how to choose between four implementation patterns — @PostConstruct, @PreDestroy, InitializingBean, and DisposableBean — based on your use case.
-
Spring @Bean "Name" Guide - When to Set It? How Does It Work? What's the Priority?
A practical guide to the role of the 'name' attribute on Spring Boot's @Bean. Covers default naming conventions (method name = Bean name), how to assign explicit names/aliases, the priority of @Qualifier, @Primary, and parameter names, avoiding Bean name collisions, and tips for using constants, all with code examples.
-
What is @Configuration / @Bean in Spring Boot? Explaining the Difference from @Component and How to Use Them
Explains the difference between @Configuration and @Bean in Spring Boot and when to use them versus @Component, with practical examples. Covers registering Beans for external libraries, argument injection into @Bean methods, CGLIB proxy behavior, and how to swap implementations in tests, along with decision criteria.