Consider a Spring bean annotated with @Scope("prototype"). What happens when you request this bean multiple times from the application context?
import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope("prototype") public class MyBean { } // In main or test: MyBean bean1 = context.getBean(MyBean.class); MyBean bean2 = context.getBean(MyBean.class); System.out.println(bean1 == bean2);
Think about how prototype scope differs from singleton scope in Spring.
Beans with @Scope("prototype") create a new instance every time they are requested from the context. So, bean1 and bean2 are different objects, making bean1 == bean2 false.
Choose the correct way to specify singleton scope using the @Scope annotation in Spring.
Singleton is the default scope but can be explicitly declared.
The correct scope name for singleton beans is "singleton". Other values like "single" or "global" are invalid.
Given a Spring bean annotated with @Scope("request"), what happens when you try to get this bean in a non-web Spring application?
import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope("request") public class RequestBean { } // In main or test: RequestBean bean = context.getBean(RequestBean.class); System.out.println(bean);
Consider what 'request' scope means and where it is valid.
The 'request' scope is only valid in a web-aware Spring context. Trying to get a request-scoped bean outside a web request context causes an exception.
Examine the code below and explain why it causes an error when run in a console (non-web) Spring Boot application.
import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope("session") public class SessionBean { } // In main or test: SessionBean bean = context.getBean(SessionBean.class); System.out.println(bean);
Think about what session scope means and where it is applicable.
Session scope beans require an active HTTP session, which only exists in web applications. Without it, Spring throws an error when trying to create such a bean.
Consider a singleton-scoped Spring bean that has a dependency on a prototype-scoped bean injected via constructor or field injection. What is the behavior of the prototype bean inside the singleton?
import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope("prototype") public class PrototypeBean { } @Component @Scope("singleton") public class SingletonBean { private final PrototypeBean prototypeBean; public SingletonBean(PrototypeBean prototypeBean) { this.prototypeBean = prototypeBean; } public PrototypeBean getPrototypeBean() { return prototypeBean; } } // In main or test: SingletonBean singleton = context.getBean(SingletonBean.class); PrototypeBean p1 = singleton.getPrototypeBean(); PrototypeBean p2 = singleton.getPrototypeBean(); System.out.println(p1 == p2);
Think about when Spring creates prototype beans during injection.
When a prototype bean is injected into a singleton, Spring creates the prototype bean once at injection time. The singleton holds that same instance, so repeated calls return the same object.