Saturday, August 25, 2012

Spring Interview Questions - 1

Spring is one of the most widely used framework in the Java/J2EE application. Almost all the job postings / consultancy requirements list Spring as one of the key skills. So it is no surprise that most of the interviews contains a wide list of Spring related questions. Here we will try to list some of the basic questions asked on the Spring Framework.
Question 1: What is Spring Framework?
Spring is an open source lightweight inversion of control application framework that supports the various aspects of application development.

Question 2: What are its features?
Spring has the following features:
  • Lightweight
  • Inversion of Control - A component lists its dependencies rather than looking for dependent objects.
  • Container - Contains and manages the lifecycle of various application components.
  • Aspect Oriented - Cross cutting concerns are handled separately through AOP module.
  • Transaction Management - Spring container provides an abstract layer of components to handle the transaction management thus allowing the developers to concentrate more on the business logic and allowing them to demarcate the transactions without worrying about the low-level issues, by providing plugable transaction managers. The spring transaction management can be run in a container less environment as well.  
  • JDBC Handling - Spring can be easily integrated with the Hibernate, JDO and iBATIS.
  • MVC Framework - Spring comes with a MVC framework as well which can use various presentation technologies like JSP, JSF, Velocity and Tiles etc. However, if you wish you to use some other MVC framework spring can be easily integrated with them as well.
Question 3: What is Inversion of Control / IoC?
Inversion of Control or IoC is a phenomenon used by frameworks to invert the control. Basically, in traditional programming the user / developer control the calling of a procedure, however when using IoC the framework controls the calling mechanism. Hence, saying simply that the framework calls us, we don't call it. Although the above statement state the calling of procedure, this can be used for any development term like instantiation etc. A better and elaborated discussion can be found here by Martin Fowler. Servlets, EJB, template method pattern can be stated as examples of Inversion of Control, in general they are Inverting the control. (Hint: Don't call us, we will call you.)

Question 4: How is Inversion of Control different from Dependency Injection?
Inversion of Control is a principle that is being followed in many frameworks, however dependency injection in one such patter that is built on top of IoC. IoC is generally followed in all framework as they are trying to invert some sort of control, however every framework may not provide you with dependency injection. In dependency injection it is the dependency creation control that is being inverted. In DI, rather than object creating the dependencies the framework will invert the control and inject dependencies. More on this can be found here.

Question 5: What pattern does Spring uses to create beans?
Generally there are lot of design patterns used in Spring to create beans. For Example,
  • Proxy interface, in case of AOP
  • Singleton, beans created in spring are singleton by default
  • Template method, in case of JdbcTemplate, JmsTemplate etc.
  • Abstract Factory pattern, is use to register and then instantiate various beans defined in the Configuration file.
Question 6: What are the ways to inject beans in Spring and which one should be used where?
In Spring beans can be inject by the setter method or by constructor. In Setter Injection, the bean is created using the default constructor or no-arg constructor and then the setter methods are called in to inject various beans. In Constructor Injection, the bean is created using the Constructed with a number of arguments and then these arguments can be inject while instantiation. Now the question arises which one should be used. IMHO I think we should use a combination of both. You should have a constructor with the "Required" arguments or collaborators and should have setter injection for dependencies which are optional. In this way, you are also making sure that you have all the required dependencies during its instantiation and optional properties can be set using setter injection. You can refer to the following blog at springsource to learn more about the above reason.

Question 7: What is JdbcTemplate in Spring DAO?
JDBCTemplate is the central class in the Spring DAO module, it has the template methods to open and close various resources, hence decreasing the programming errors. This class provides the basic workflow for a JDBC like opening the connection, preparing the statement , executing and then closing the connection thus leaving the application code to only provide the SQL and retrieve the results. It has methods for executing SQL queries, update statements and procedure calls. It also provides the methods for iterating over a result set. This class also catches SQL Exceptions and generates more informative exceptions defined in the Spring Exception hierarchy.