Top Spring interview questions and answers - Part 1

In this article, I'll discuss some of the most important Spring interview questions

1. What is the difference between Spring singleton and singleton design pattern?

The Java singleton is scoped by the Java class loader whereas the Spring singleton is scoped by the container context. Which basically means that, in Java, you can be sure a singleton only within the context of the class loader which loaded it. Other class loaders should be capable of creating another instance of it. So, Java considers a singleton in a way that it cannot create more than one instance of that class within a given class loader, whereas Spring considers a singleton in a way that it cannot create more than one instance of a class within a given container/context.


2. What are the benefits of IOC?

The main benefits of IOC are:

  • It minimizes the amount of code in your application.
  • It makes your application easy to test as it doesn't require any singletons or JNDI lookup mechanisms in your unit test cases.
  • Loose coupling is promoted with minimal effort.
  • IOC containers support eager instantiation and lazy loading of services.

3. How to configure Spring using Annotation?

By default, Annotation wiring is not turned on in the Spring container. So, before you can use annotation-based wiring, you need to enable it in your Spring configuration file. Using <context:annotation-config> you can enable annotation in your Spring application.
Example:

                      <?xml version="1.0" encoding="UTF-8"?>
                         <beans xmlns="http://www.springframework.org/schema/beans"
                          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                          xmlns:context="http://www.springframework.org/schema/context"
                          xsi:schemaLocation="http://www.springframework.org/schema/beans
                          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                          http://www.springframework.org/schema/context
                          http://www.springframework.org/schema/context/spring-context-3.0.xsd">

                          <context:annotation-config/>
                          <!-- bean definitions go here -->

                        </beans>

4. What is Aspect Oriented Programming?

Aspect-oriented programming is a programming technique that allows programmers to modularize crosscutting concerns, or behavior that cuts across the typical divisions of responsibility, such as logging, transaction management, data validation and authentication. In Object Oriented Programming, modularity of application is achieved by Classes whereas in AOP application modularity is achieved by Aspects and they are configured to cut across different classes methods.
AOP takes out the direct dependency of cross-cutting tasks from classes that is not possible in normal object-oriented programming. For example, we can have a separate class for logging but again the classes will have to call these methods for logging the data.
The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules.


5. What are different modules in Spring?

Following are the different modules in Spring:

  • The Core container module
  • Application context module
  • AOP module (Aspect Oriented Programming)
  • JDBC abstraction and DAO module
  • O/R mapping integration module (Object/Relational)
  • Web module
  • MVC framework module

6. What is Spring IoC container?

The Spring IoC is responsible for creating the objects, managing them i.e injecting dependency, wiring them together, configuring them, and also managing their complete lifecycle.Inversion of Control (IoC) is the mechanism to achieve loose-coupling between Objects dependencies. To achieve loose coupling and dynamic binding of the objects at runtime, the objects define their dependencies that are being injected by other assembler objects. Spring IoC container is the program that injects dependencies into an object and make it ready for our use.

The basic concept of the Inversion of Control pattern (also known as dependency injection) is that you do not create your objects but describe how they should be created. You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file.


7. What is default scope of bean in Spring framework?

The default scope of bean is "Singleton" in Spring framework.


8. Can you instantiate a static inner class in Spring?

If for whatever reason you want to configure a bean definition for a static inner class, you have to use the binary name of the inner class.

For example, if you have a class called Foo in the com.example package, and this Foo class has a static inner class calledBar, the value of the 'class' attribute on a bean definition would be...

com.example.Foo$Bar

Notice the use of the $ character in the name to separate the inner class name from the outer class name.


9. What is an advice?

Advice is the action taken by an action at a particular join point. In other words, this is an actual piece of code that is invoked when a certain join point with matching pointcut is reached in the application.
For example Implementation for logging/Log4j.


10. What are the types of the transaction management Spring supports?

Spring Framework supports:

  • Programmatic transaction management.
  • Declarative transaction management.

In this article, we discussed some of the most frequently asked Spring interview questions.

To read more Spring interview question visit Spring Interview Question

Attempt Spring Multiple Choice Question at Spring MCQ

Author
Author: Amit Gupta
Published On: 19/09/2015
Last revised On: 30/09/2018
View all articles by Amit Gupta

Share this post

Comments

Comments
comments powered by Disqus

Navigation

Social Media