Feb 13, 2014

Understanding Spring scopes -- Singleton Vs Prototype

Spring Interview Questions and Answers Q1 - Q14 are FAQs

Q1 - Q4 Overview & DIP Q5 - Q8 DI & IoC Q9 - Q10 Bean Scopes Q11 Packages Q12 Principle OCP Q14 AOP and interceptors
Q15 - Q16 Hibernate & Transaction Manager Q17 - Q20 Hibernate & JNDI Q21 - Q22 read properties Q23 - Q24 JMS & JNDI Q25 JDBC Q26 Spring MVC Q27 - Spring MVC Resolvers

You will be hard pressed to find a Java project that does not use Spring, hence it pays to know its fundamentals. These questions and answers on Spring scopes are often asked in good job interviews.

Q. Does Spring dependency injection happen during compile time or runtime?
A. Runtime during creating an object.

Q9. What is the difference between prototype scope and singleton scope? Which one is the default?
A9. Singleton means single bean instance per IoC container, and prototype means any number of object instances per IoC container. The default scope is "singleton".

Q. When will you use singleton scope? When will you use prototype scope?
A. Singleton scope is used for stateless object use. For example, injectiong a DAO (i.e. Data Access Object) into a service object. DAOs don't need to maintain conversation state. For example,

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <bean id="myDaoDef" class="com.mycompany.understanding.spring.MyDaoImpl" scope="singleton"/>
    <bean id="myServiceDef" class="com.mycompany.understanding.spring.MyServiceImpl" scope="singleton"> 
       <constructor-arg name="myDao" ref="myDaoDef" /> 


Prototype is useful when your objects maintain state in a multi-threaded environment. Each thread needs to use its own object and cannot share the single object. For example, you might hava a RESTFul web service client making multi-threaded calls to Web services. The REST easy client APIs like RESTEasy uses the Apache Connection manager which is not thread safe and each thread should use its own client. Hence, you need to use the prototype scope.

Q. Would both singleton and prototype bean's life cycle be managed by the Spring IoC container?
A. Yes and no. The singleton bean's complete life cycle will be managed by Spring IoC container, but with regards to prototype scope, IoC container only partially manages the life cycle - instantiates, configures, decorates and otherwise assembles a prototype object, hands it to the client and then has no further knowledge of that prototype instance. As per the spring documentation

"This means that while initialization lifecycle callback methods will be called on all objects regardless of scope, in the case of prototypes, any configured destruction lifecycle callbacks will not be called. It is the responsibility of the client code to clean up prototype scoped objects and release any expensive resources that the prototype bean(s) are holding onto."

Q. What happens if you inject a prototype scoped bean into a singleton scoped bean?
A. A new prototype scoped bean will be injected into a singleton scoped bean once at runtime, and the same prototype bean will be used by the singleton bean.

Q. What if you want the singleton scoped bean to be able to acquire a brand new instance of the prototype-scoped bean again and again at runtime?
A. In this  use-case, there is no use in just dependency injecting a prototype-scoped bean into your singleton bean, because as stated above, this only happens once when the Spring container is instantiating the singleton bean and resolving and injecting its dependencies. You can just inject a singleton (e.g. a factory) bean and then use Java class to instantiate (e.g with a newInstance(...) or create(..) method) a new bean again and again at runtime without relying on Spring or alternatively have a look at Spring's "method injection". As per Spring documentation for "Lookup method injection"

"Lookup method injection refers to the ability of the container to override methods on container managed beans, to return the result of looking up another named bean in the container. The lookup will typically be of a prototype bean as in the scenario described above. The Spring Framework implements this method injection by dynamically generating a subclass overriding the method, using bytecode generation via the CGLIB library."

Q10. What are the scopes defined in HTTP context?
A10. Following scopes are only valid in the context of a web-aware Spring ApplicationContext.

  • request Scope is for a single bean definition to the lifecycle of a single HTTP request.In other words each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. 
  • session Scope is for a single bean definition to the lifecycle of a HTTP Session. 
  • global session Scope is for a  single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. 

Q. Does Spring allow you to define your own bean scopes?
A. Yes, from Spring 2.0 onwards you can define custom scopes. For example,
  • You can define a ThreadOrRequest and ThreadOrSession scopes to be able to switch between the environment you run in like JUnit for testing and Servlet container for running as a Web application. 
  • You can write a custom scope to inject stateful objects into singleton services or factories.
  • You can write a custom bean scope that would create new instances per each JMS message consumed
  • Oracle Coherence has implemented a datagrid scope for Spring beans. You will find many others like this.

In the next post, I will demonstrate "singleton" and prototype scopes with tutorial like code, Stay tuned.

Relevant tutorials:

Labels: ,


Post a Comment

Subscribe to Post Comments [Atom]

<< Home