Spring Interview Questions
Spring Interview Questions Index
What is Spring framework ?
What is Dependency Injection/Inversion Of Control(IOC) in Spring framework ?
What are the different types of Inversion of Control or dependency injection ?
What are the different types of Spring dependency injection ?
What are the advantages or Pros of IOC (Dependency Injection) ?
What are the pros or benefits of Spring framework ?
What are the various modules in Spring?
Spring framework is an open source Java application framework created to ease developers effort by addressing the enterprise application development complexity. The beauty of Spring framework is it’s layered architecture which allows the programmer to be selective about which all spring components he wants. And Spring also provides a cohesive framework for Java Enterprise application development. Spring framework comprises of several modules that provide different services such as Spring IOC(dependency injection)framework, Spring MVC, Spring data access framework, Spring validator framework etc. In fact, it’s a common practice in Software industry to use struts MVC together with spring IOC framework, so whenever you mention about Spring framework you can be more specific like spring IOC framework or Spring MVC framework.
2. What is Dependency Injection/Inversion Of Control(IOC) in Spring framework ?
You can expect this question, this one among the common Spring Interview Questions.
The basic concept of the Dependency Injection or Inversion of Control is that, programmer do not need to create the objects, instead just describe how it should be created. No need to directly connect your components and services together in program, instead just describe which services are needed by which components in a configuration file/xml file. The Spring IOC container is then responsible for binding it all up.
In other words, while applying Inversion Of Control, at the time of object creation, objects are given their dependencies by some external entity that coordinates each object in the system. That means, dependencies are injected into objects at the time of their creation. So, Inversion of Control means an inversion of responsibility with regard to how an object obtains references to collaborating objects.
3. What are the different types of Inversion of Control or dependency injection ?
There are three different types of Inversion of Control or dependency injection:
- Setter Injection: Dependencies are injected through JavaBeans properties (ex: setter/Getter methods in bean objects).
- Constructor Injection: Dependencies are assigned as constructor parameters.
- Interface Injection: Injection is done through an interface.
As I said earlier, its Setter Injection and Constructor Injection
- Setter Injection: Dependencies are provided through JavaBeans properties (ex: setter/Getter methods in bean objects).
- Constructor Injection: Dependencies are assigned as constructor parameters.
Advantages of Dependency Injection/Inversion of Control are as follows:
- Dependency Injection minimizes the amount of code in any application. Dependency is handled by the framework itself
- Dependency Injection makes developers life easier. With Inversion of Control containers developers do not need to think about how services are created and how to get references to the ones he needs.
- Easily scalable applications. It’s very easy to add additional services by adding a new constructor or a getter/setter method with a minimal configuration. With Spring Framework 3.0, its even easier as <context:component-scan base-package=”com.blah.blah”/> will do everything for you, you don’t need to add getter and setter method and beans for each dependency injection, just autowire the services wherever it needed.<Read how spring 3.0 made a developers life easier>
- Dependency Injection makes your application more test-friendly by not demanding any JNDI lookup mechanisms or singletons in your test cases. IOC containers make testing and switching implementations easy by allowing you to inject your own objects into the object under test.
- Comparing to other options like factory design pattern the IOC container is injecting the dependency into requesting piece of code where as the factory design pattern is more intrusive and components or services need to be requested explicitly.
- IOC containers support eager instantiation and lazy loading of services.
- IOC Containers provide support for instantiation of cyclical dependencies, managed objects, life cycles management and dependency resolution between managed objects etc.
This is one among the common Spring Interview Questions
The pros of Spring framework are as follows:
- Spring is an open source framework and free to download.
- Spring has layered architecture. You can select the feature you wants, as I said before you can have Struts MVC and Springs IOC container in one application itself. Eventhough spring has MVC framework if you want you can opt out.
- Spring Enables Plain Old Java Object (POJO) Programming. POJO programming enables continuous integration and testability.
- Dependency Injection is really cool stuff, spring 3.0 onwards the introduction of component-scan/autowiring and Spring Expression Language makes it even spicier.
- spring is lightweight.
Spring comprises of seven different modules. They are as follows
Inversion of Control container/ The core container
The core container/IoC container handles the configuration of application components and lifecycle management of Java objects. It is responsible for the Dependency Injection. Inversion of Control container provides the essential functionality of the Spring framework. A primary component of the core container is the BeanFactory, an implementation of the Factory pattern. The BeanFactory applies the IOC pattern to separate an application’s actual application code and configuration and dependency specification.
The Spring context is a configuration file that provides context information to the Spring framework. The Spring context includes enterprise services such as JNDI, EJB, e-mail, internalization, validation, and scheduling functionality.
Spring AOP -Aspect oriented programming
The Spring AOP module integrates aspect-oriented programming functionality directly into the Spring framework through its configuration management feature. Spring AOP enables implementation of cross-cutting routines. As a result you can easily enable aspect oriented programming in any object managed by the Spring framework. The Spring AOP module provides transaction management services for objects in any Spring-based application. With Spring AOP you can incorporate declarative transaction management into your applications without relying on EJB components.
Spring DAO -Data access
The Spring JDBC DAO abstraction layer offers an exception hierarchy for managing the exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the amount of exception code you need to write, such as opening and closing connections. Spring DAO’s JDBC-oriented exceptions comply to its generic DAO exception hierarchy.Spring DAO is working with relational database management systems on the Java platform using JDBC and object-relational mapping tools and with NoSQL databases
The Spring framework plugs into several ORM frameworks to provide its Object Relational tool, including JDO, Hibernate, and iBatis SQL Maps. All of these comply to Spring’s generic transaction and DAO exception hierarchies.
Spring Web module
The Web context module builds on top of the application context module, providing contexts for Web-based applications. As a result, the Spring framework supports integration with Jakarta Struts. The Web module also eases the tasks of handling multi-part requests and binding request parameters to domain objects.
Spring MVC framework
The Model-View-Controller (MVC) framework is a full-featured MVC implementation for building Web applications. The MVC framework is highly configurable via strategy interfaces and accommodates numerous view technologies including JSP, Velocity, Tiles, iText, and POI.