Spring Top

I just announced the new Spring Boot 2 material, coming in REST With Spring:


Table of Contents

1. Introduction

In this article, we’re going to look at some of the most common Spring-related questions that might pop up during a job interview.

2. Spring Core

Q1. What is Spring Framework?

Spring is the most broadly used framework for the development of Java Enterprise Edition applications. The core features of Spring can be used in developing any Java application.

We can use its extensions for building various web applications on top of the Java EE platform, or we may just use its dependency injection provisions in simple standalone applications.

Q2. What are the benefits of using Spring?

Spring targets to make Java EE development easier. Here are the advantages of using it:

  • Lightweight: there is a slight overhead of using the framework in development
  • Inversion of Control (IoC): Spring container takes care of wiring dependencies of various objects, instead of creating or looking for dependent objects
  • Aspect Oriented Programming (AOP): Spring supports AOP to separate business logic from system services
  • IoC container: it manages Spring Bean life cycle and project specific configurations
  • MVC framework: that is used to create web applications or RESTful web services, capable of returning XML/JSON responses
  • Transaction management: reduces the amount of boiler-plate code in JDBC operations, file uploading, etc., either by using Java annotations or by Spring Bean XML configuration file
  • Exception Handling: Spring provides a convenient API for translating technology-specific exceptions into unchecked exceptions

Q3. What Spring sub-projects do you know? Describe them briefly.

  • Core – a key module that provides fundamental parts of the framework, like IoC or DI
  • JDBC – this module enables a JDBC-abstraction layer that removes the need to do JDBC coding for specific vendor databases
  • ORM integration – provides integration layers for popular object-relational mapping APIs, such as JPA, JDO, and Hibernate
  • Web – a web-oriented integration module, providing multipart file upload, Servlet listeners, and web-oriented application context functionalities
  • MVC framework – a web module implementing the Model View Controller design pattern
  • AOP module – aspect-oriented programming implementation allowing the definition of clean method-interceptors and pointcuts

Q4. What is Dependency Injection?

Dependency Injection, an aspect of Inversion of Control (IoC), is a general concept stating that you do not create your objects manually but instead describe how they should be created. An IoC container will instantiate required classes if needed.

For more details, please refer here.

Q5. How can we inject beans in Spring?

A few different options exist:

  • Setter Injection
  • Constructor Injection
  • Field Injection

The configuration can be done using XML files or annotations.

For more details, check this article.

Q6. Which is the best way of injecting beans and why?

The recommended approach is to use constructor arguments for mandatory dependencies and setters for optional ones. Constructor injection allows injecting values to immutable fields and makes testing easier.

Q7. What is the difference between BeanFactory and ApplicationContext?

BeanFactory is an interface representing a container that provides and manages bean instances. The default implementation instantiates beans lazily when getBean() is called.

ApplicationContext is an interface representing a container holding all information, metadata, and beans in the application. It also extends the BeanFactory interface but the default implementation instantiates beans eagerly when the application starts. This behavior can be overridden for individual beans.

For all differences, please refer to the reference.

Q8. What is a Spring Bean?

The Spring Beans are Java Objects that are initialized by the Spring IoC container.

Q9. What is the default bean scope in Spring framework?

By default, a Spring Bean is initialized as a singleton.

Q10. How to define the scope of a bean?

To set Spring Bean’s scope, we can use @Scope annotation or “scope” attribute in XML configuration files. There are five supported scopes:

  • singleton
  • prototype
  • request
  • session
  • global-session

For differences, please refer here.

Q11. Are singleton beans thread-safe?

No, singleton beans are not thread-safe, as thread safety is about execution, whereas the singleton is a design pattern focusing on creation. Thread safety depends only on the bean implementation itself.

Q12. What does the Spring bean lifecycle look like?

First, a Spring bean needs to be instantiated, based on Java or XML bean definition. It may also be required to perform some initialization to get it into a usable state. After that, when the bean is no longer required, it will be removed from the IoC container.

The whole cycle with all initialization methods is shown on the image (source):

Q13. What is the Spring Java-Based Configuration?

It’s one of the ways of configuring Spring-based applications in a type-safe manner. It’s an alternative to the XML-based configuration.

Also, if you want to migrate your project from XML to Java config, please refer to this article.

Q14. Can we have multiple Spring configuration files in one project?

Yes, in large projects, having multiple Spring configurations is recommended to increase maintainability and modularity.

You can load multiple Java-based configuration files:

@Import({MainConfig.class, SchedulerConfig.class})
public class AppConfig {

Or load one XML file that will contain all other configs:

ApplicationContext context = new ClassPathXmlApplicationContext("spring-all.xml");

And inside this XML file you’ll have:

<import resource="main.xml"/>
<import resource="scheduler.xml"/>

Q15. What is Spring Security?

Spring Security is a separate module of the Spring framework that focuses on providing authentication and authorization methods in Java applications. It also takes care of most of the common security vulnerabilities such as CSRF attacks.

To use Spring Security in web applications, you can get started with a simple annotation: @EnableWebSecurity.

You can find the whole series of articles related to security on Baeldung.

Q16. What is Spring Boot?

Spring Boot is a project that provides a pre-configured set of frameworks to reduce boilerplate configuration so that you can have a Spring application up and running with the smallest amount of code.

Q17. Name some of the Design Patterns used in the Spring Framework?

  • Singleton Pattern: Singleton-scoped beans
  • Factory Pattern: Bean Factory classes
  • Prototype Pattern: Prototype-scoped beans
  • Adapter Pattern: Spring Web and Spring MVC
  • Proxy Pattern: Spring Aspect Oriented Programming support
  • Template Method Pattern: JdbcTemplate, HibernateTemplate, etc.
  • Front Controller: Spring MVC DispatcherServlet
  • Data Access Object: Spring DAO support
  • Model View Controller: Spring MVC

Q18. How does the scope Prototype work?

Scope prototype means that every time you call for an instance of the Bean, Spring will create a new instance and return it. This differs from the default singleton scope, where a single object instance is instantiated once per Spring IoC container.

3. Spring Web MVC

Q19. How to Get ServletContext and ServletConfig Objects in a Spring Bean?

You can do either by:

  1. Implementing Spring-aware interfaces. The complete list is available here.
  2. Using @Autowired annotation on those beans:
ServletContext servletContext;

ServletConfig servletConfig;

Q20. What is a Controller in Spring MVC?

Simply put, all the requests processed by the DispatcherServlet are directed to classes annotated with @Controller. Each controller class maps one or more requests to methods that process and execute the requests with provided inputs.

If you need to take a step back, we recommend having a look at the concept of the Front Controller in the typical Spring MVC architecture.

Q21. How does the @RequestMapping annotation work?

The @RequestMapping annotation is used to map web requests to Spring Controller methods. In addition to simple use cases, we can use it for mapping of HTTP headers, binding parts of the URI with @PathVariable, and working with URI parameters and the @RequestParam annotation.

More details on @RequestMapping are available here.

For more Spring MVC questions, please check out Spring MVC Interview Questions article.

4. Spring Data Access

Q22. What is Spring JDBCTemplate class and how to use it?

The Spring JDBC template is the primary API through which we can access database operations logic that we’re interested in:

  • creation and closing of connections
  • executing statements and stored procedure calls
  • iterating over the ResultSet and returning results

To use it, we’ll need to define the simple configuration of DataSource:

public class SpringJdbcConfig {
    public DataSource mysqlDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        return dataSource;

For further explanation, you can go through this quick article.

Q23. How would you enable transactions in Spring and what are their benefits?

There are two distinct ways to configure Transactions – with annotations or by using Aspect Oriented Programming (AOP) – each with their advantages.

The benefits of using Spring Transactions, according to the official docs, are:

  • Provide a consistent programming model across different transaction APIs such as JTA, JDBC, Hibernate, JPA, and JDO
  • Support declarative transaction management
  • Provide a simpler API for programmatic transaction management than some complex transaction APIs such as JTA
  • Integrate very well with Spring’s various data access abstractions

Q24. What is Spring DAO?

Spring Data Access Object is Spring’s support provided to work with data access technologies like JDBC, Hibernate, and JPA in a consistent and easy way.

You can, of course, go more in-depth on persistence, with the entire series discussing persistence in Spring.

5. Spring Aspect-Oriented Programming (AOP)

Q25. What is Aspect-Oriented Programming?

Aspects enable the modularization of cross-cutting concerns such as transaction management that span multiple types and objects by adding extra behavior to already existing code without modifying affected classes.

Here is the example of aspect-based execution time logging.

Q26. What are Aspect, Advice, Pointcut, and JoinPoint in AOP?

  • Aspect: a class that implements cross-cutting concerns, such as transaction management
  • Advice: the methods that get executed when a specific JoinPoint with matching Pointcut is reached in the application
  • Pointcut: a set of regular expressions that are matched with JoinPoint to determine whether Advice needs to be executed or not
  • JoinPoint: a point during the execution of a program, such as the execution of a method or the handling of an exception

Q27. What is Weaving?

According to the official docs, weaving is a process that links aspects with other application types or objects to create an advised object. This can be done at compile time, load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.

6. Conclusion

In this extensive article, we’ve explored some of the most important questions for a technical interview all about Spring.

We hope that this article will help you in your upcoming Spring interview. Good luck!

« Previous
Java Annotations Interview Questions (+ Answers)
Spring bottom

I just announced the new Spring Boot 2 material, coming in REST With Spring:


newest oldest most voted
Notify of

Very helpful during interivews, You have explain all the required senario during inteview,
Could you please share some set of question on SPring boot and Spring Rest full webservice also.
thanks in advance