@Scope for bean scope in Spring Boot
📖 Scenario: You are building a simple Spring Boot application that manages user sessions. You want to control how Spring creates and manages your beans using the @Scope annotation.This helps you decide if a bean should be created once for the whole app or a new one each time it is needed.
🎯 Goal: Learn how to use the @Scope annotation to define bean scopes in Spring Boot.You will create a bean with singleton scope and then change it to prototype scope.
📋 What You'll Learn
Create a Spring Boot bean class named
MyBeanAdd
@Component annotation to MyBeanAdd
@Scope annotation with ConfigurableBeanFactory.SCOPE_SINGLETON to MyBeanChange the scope to
ConfigurableBeanFactory.SCOPE_PROTOTYPE using @Scope💡 Why This Matters
🌍 Real World
Controlling bean scope is important in real applications to manage resources and behavior, like creating a new bean for each user request or sharing one bean for the whole app.
💼 Career
Understanding <code>@Scope</code> is essential for Spring developers to write efficient, maintainable, and scalable applications.
Progress0 / 4 steps