Spring Interview Questions List in 2020

There are two ways to go through Spring Interview Questions for a Java developer:

  1. If you are in a hurry (on the way to your interview or just want to revise), then go through our FAST TRACK questions and answers on Spring in the current article which covers everything on every Spring project.
  2. But if you have a bit of time, even A DAY, then go through our Spring Project’s specific articles, which will make you even more confident. These articles answers the questions in a bit detailed manner.

Treat this article as a master-index for our series of articles on Spring Interview Questions. Bookmark this article as your go to reference guide for any Spring Interview.

Spring Topic Specific Interview Questions

Spring Interview Questions And Answers

Spring Interview Questions And Answers

Let’s start our Q&A marathon.

Basic Spring Interview Questions

1. What is Spring?

As a name, Spring, is an umbrella term used to represent a cluster of open-source projects( for enterprise application development) under the common leadership of Pivotal Software.

Also, sometimes we call Spring Framework project as Spring.

2. What are some new features in Spring 5?

  1. Minimum JDK version required – JDK 8
  2. Minimum JEE version required – JEE7
  3. Removed support for Portlet, Velocity, JasperReports, XMLBeans, JDO, Guava.
  4. Introduction of Spring WebFlux for Reactive programming.

3. Is Spring 5 compatible with older version of Java?

If we have older than Java 8 version running, then it is not compatible. We need to upgrade. For Java 8+, it is OK.

4. Is Spring 5 compatible with Java 9 modularity feature?

Yes. Spring 5 is well tested with Java 9 modularity.

5. What is the difference between Spring vs Spring MVC vs Spring Boot?

Spring is common name for all Spring projects.

Spring MVC is part of Spring Framework project.

Spring Boot is a Spring project – which might use Spring MVC if required.

Spring IoC Interview Questions And Answers

6. What is Spring IoC design pattern?

IoC is a design pattern in which we transfer the control of something, to a framework. With Spring IoC, we transfer the control of ‘Object creation and assembling’ to Spring IoC container.

7. What are the advantages of Spring IoC?

  1. Loose Coupling
  2. Better Code testing.

8. What is Spring IoC container?

Spring IoC container does the object creation and assembly of objects with dependency injection. It does with the help of configuration metadata given to it.

spring ioc container with configuration metadata

9. What are different types of IoC containers?

  1. Bean Factory
  2. Application Context

10. What is bean factory?

BeanFactory is the most parent container interface which facilitates:

  1. Loading of Bean Definitions
  2. Dependency Injection

11. Provide one BeanFactory interface implementation example.

XMLBeanFactory class.

12. What is ApplicationContext?

Spring ApplicationContext is a child interface of BeanFactory.

13. What is the difference between BeanFactory vs ApplicationContext?

BeanFactoryApplicationContext
Most Parent Interface.Child Interface of BeanFactory.
Supports Bean Definition loading and Dependency Injection.Supports BeanFactory functions + Event Publishing + File resource loading.

14. Explain Application Context Hierarchy.

Spring Application Context Hierarchy

15. What are most popular ApplicationContext implementations?

  1. FileSystemXmlApplicationContext
  2. ClassPathXmlApplicationContext
  3. AnnotationConfigApplicationContext
  4. WebXmlApplicationContext
  5. AnnotationConfigWebApplicationContext

16. What is Dependency Injection?

Dependency injection is the mechanism by which Spring IoC container injects the Spring Beans it created into code.

17. What is the difference between DI vs IoC?

In the context of Spring, it is one and same thing. But outside of Spring arena, IoC is generic term where ‘any control’ can be transfered to outside framework.

Dependency Injection is one of that control, which is specific to how dependency will be injected, is outsourced to Spring Framework.

18. What are the advantages of Dependency Injection?

  1. Loose Coupling
  2. Better Code Testing.

19. In how many ways we can do dependency injection?

  1. Constructor-based DI
  2. Setter based DI
  3. Method Injection

20. Is Interface Injection possible in Spring IoC?

Spring does not support interface injection.

21. What is the difference between Constructor vs Setter injection?

Constructor DISetter DI
Dependency provided through constructors.Dependency provided through setter methods.
All dependencies are mandatory.Not mandatory.
If there is no bean for even one of the constructor parameter, exception will be thrown. If there is no bean for a setter where dependency is expected, nothing will happen.
Can result in circular dependencies.Used as one of the solutions for circular dependencies.

22. Which exception occurs at the time of circular dependency?

ObjectCurrentlyInCreationException

23. Can we use both constructor and setter DI?

Yes. We can mix and match.

24. What is method injection?

In this, we inject methods, instead of objects.

25. What are different types of method Injection?

  1. Lookup method injection
  2. Arbitrary method replacement

26. What is lookup method injection?

With this, we override one or more methods of a container managed bean from the container itself.

27. What is Arbitrary method replacement?

With this, we replace the methods of container managed beans.

28. Can you inject null and empty String?

Yes.

29. What is Spring Bean?

Object(s) managed by ioc container.

30. Could you explain spring bean life cycle?

Interview question on spring bean life cycle

31. What is Spring IoC container configuration file?

This file has metadata regarding all the bean definitions.

32. In how many ways we can provide the metadata to the ioc container?

  1. By XML – XML based configuration
  2. Marking Annotations – Annotation based configuration
  3. Java based configuration

33. Can we have multiple configuration file in a Spring IoC container?

Yes, we can.

34. How to inject a Properties file in a Spring bean?

With the help of a PropertyConfigurer like this:

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer>
    <property name="location" value="/path/to/application.properties" />
</bean> 

<bean class="path.to.SomeClass">
    <property name="someClassProperty" value="${propertyKey}"/>
</bean>

35. What are different bean scopes in Spring IoC container?

  1. singleton
  2. prototype
  3. request
  4. session
  5. application
  6. websocket
  7. custom

36. By default, what is the scope of Spring bean?

Singleton.

37. Are singleton beans thread safe?

No. And also, it depends upon how we are using it.

38. How Singleton scope works?

Same object per ApplicationContext for every injection.

How Singleton scope works in Spring?

39. How prototype scope works?

New object for each injection.

How prototype scope works in spring

40. What is Spring bean autowiring?

Spring bean autowiring is a mechanism by which container itself selects which beans to inject and where. We just add the @Autowire annotation to declare a dependency.

41. What are different modes of autowiring?

interview question on different spring bean autowiring modes

42. How to enable autowiring?

<context:annotation-config/>

Add this in the XML metadata file.

43. What is @Autowired annotation?

@Autowired annotation is used to declare the point of injection.

44. What is @Qualifier annotation?

@Qualifier annotation is just to resolve the ambiguity in selecting the bean to inject.

45. What is @Required annotation?

@Required annotation is used to declare the setter injection point as mandatory dependency.

46. What is the difference between autowiring vs dependency injection?

Autowiring is one of the technique to do dependency injection. We can do DI without autowiring just be declaring everything in the bean definitions itself.

47. What are some of the limitations of autowiring?

  1. Not very explicit
  2. Low priority than XML and Java configuration
  3. No support for primitives

48. What is component scanning?

It is a mechanism by which Spring IoC container finds the bean definitions by scanning the Java code and looking for the classes annotated with specific annotation.

49. What is the difference between <context:annotation-config> vs <context:component-scan> ?

Annotation config just resolves the annotations while components scan does the following:

  1. Scans the packages
  2. Resolve the annotations
  3. Register the beans

50. What is application.properties file?

This file is used to configure the Spring related properties for the whole application.

51. What is JSR 250?

Under JSR 250, Java provides us certain annotations which we can use in our Spring application to mitigate the dependencies on Spring based annotations.

To see more detailed ioc questions, click here.

Spring MVC Interview Questions And Answers

52. What is Spring MVC?

Spring MVC is the implementation of Model-View-Controller(MVC) based architecture, provided by Spring.

It is part of Spring Framework project.

53. What are the advantages of Spring MVC?

  1. Separation of concern
  2. Loose Coupling
  3. Fast and easy development
  4. Easy to Unit test

54. Explain different Spring MVC components.

interview question on spring mvc architecture components

55. Explain Front Controller pattern.

In this pattern, all the incoming and outgoing HTTP request and responses, pass through a central Servlet, called Front Controller.

front controller pattern in spring

56. What is Dispatcher Servlet?

DispatcherServlet is the class which acts as Front Controller in Spring MVC.

57. What is WebApplication context?

WebApplicationContext is the ApplicationContext in the Servlet environment.

WebApplicationContext in a Spring

58. What is Controller in Spring MVC?

Controller is what handles user requests. Behind the scenes, it is a Servlet.

59. What is @Controller annotation?

@Controller annotation is used to declare a class as MVC Controller component.

60. What is @RestController annotation?

@RestController enables responses to return in JSON format, to be compatible with REST specification.

61. What is @RequestMapping annotation?

@RequestMapping annotation is used at class as well as method level, to map the HTTP request to a method body.

62. What is @RequestParam annotation?

@RequestParam annotation is used to map HTTP URL query parameters to the @Controller method parameters.

63. What is @PathVariable annotation?

@PathVariable annotation is used to map HTTP URL variable to the @Controller method parameters.

64. What is @RequestBody annotation?

@RequestBody annotation is used to map HTTP POST request body to a POJO class.

65. What is @ResponseBody annotation?

@ResponseBody annotation converts POJO class to JSON object and sets it in the HTTP response.

66. What is @EnableWebMVC annotation?

@EnableWebMVC is used to enable MVC support via. Java based configuration.

67. How to get ServletContext and ServletConfig objects in a Spring bean?

By implementing ServletContextAware and ServletConfigAware like this:

@Controller
public class AController implements ServletContextAware, ServletConfigAware {
 
    private ServletContext context;
    private ServletConfig config;
 
    @Override
    public void setServletConfig(final ServletConfig servletConfig) {
        this.config = servletConfig;
 
    }
 
    @Override
    public void setServletContext(final ServletContext servletContext) {
        this.context = servletContext;
    }
}

68. What is the difference between @Component vs @Controller vs @Service vs @Repository?

@Controller, @Service and @Repository annotations are alias of @Component annotation.

69. What is Spring MVC interceptor?

Interceptors are used to intercept HTTP requests and execute some logic. Just like Servlet filters.

70. What is ContextLoaderListener?

ContextLoaderListener is the Spring class which listenes to the ServletContext creation and loads the RootWebApplicationContext into it.

71. What is ViewResolver in Spring MVC?

ViewResolver is a special Spring MVC component which takes view name as input and translate that to exact view page.

72. What is MultipartResolver in Spring MVC?

MultipartResolver component is used to support file uploading a spring based web application.

Checkout this article for more detailed MVC interview questions and answers.

Spring AOP Interview Questions And Answers

73. What is Aspect Oriented programming (AOP) in Spring framework?

Spring AOP is used to deal with cross-cutting concerns of an application in a modular fashion. Examples of cross-cutting concerns will be: Security, Logging, Transaction Management etc.

aspect oriented programming in spring aop

74. What are advantages of Spring AOP?

  1. Modularity
  2. Pluggable
  3. Easy maintainance

75. What is an Aspect?

In terms of Spring AOP, Aspect is the cross-cutting concern we want to implement.

76. What is @Aspect annotation?

@Aspect annotation is used to declare an Aspect.

77. What is Advice in Spring AOP?

Advice is that logic which we want to execute as part of Aspect.

78. List different Spring AOP advice types.

  1. Before Advice
  2. After Returning Advice
  3. After throwing Advice
  4. After Advice
  5. Spring AOP Around Advice
spring aop advice types

79. What is a JoinPoint in Spring AOP?

JoinPoint is the target location where different configured advices will be applied.

80. What is a Pointcut in Spring AOP?

Pointcut is the expression used to select the JoinPoints.

81. Does Spring framework supports all types of JoinPoints?

Spring framework supports only method execution JoinPoint.

82. What is Weaving?

Weaving is the process of linking the aspect to the java objects, to create a target object.

Interview quuestion on how weaving works in Spring AOP?

83. What are Spring AOP proxy objects?

AOP proxy objects are the runtime objects created by Spring AOP, to apply the advices.

Spring AOP Proxy

84. List most popular AOP implementations.

  1. Spring AOP
  2. AspectJ
  3. JBoss AOP
different Spring aop implementations

85. How to enable Spring Boot AOP?

Add “spring-boot-starter-aop” starter project dependency in your Maven configuration.

Checkout this article for more detailed questions and Answers on Spring AOP.

From here starts the Spring interview questions and answers pertaining to Spring Boot.

Spring Boot Interview Questions And Answers

86. What is Spring Boot?

Spring Boot is one of the Spring projects which facilitates Rapid application development for creating production-ready enterprise applications.

In micro-service architecture, it gets very easy to build micro-services with Spring Boot.

87. What are some of the Spring Boot advantages?

  1. Rapid Application development (RAD)
  2. Minimal Configuration
  3. Out-of the box production grade features like monitoring and health checks.

88. List some of the Spring Boot features.

  1. Spring Boot Starter Projects
  2. Auto-Configuration
  3. Profiling
  4. Auto configured Logging
  5. Auto configured i18n
  6. Spring Actuator
  7. Spring Initializer
  8. Spring CLI

89. What is Spring Boot auto-configuration?

Auto-Configurations are the pre-build configurations provided by Spring Boot project which we can just apply.

In this way, it helps mitigate the configuration required by a Spring application.

90. What are different ways to create a Spring Boot application?

  1. By Spring Initializer tool
  2. By Spring Boot CLI
  3. With the help of Spring Starter project Wizard
  4. As a Simple Maven project

91. How can we change the Spring Boot embedded tomcat port number?

server.port=8085

Add the above in application.properties file and that’s it.

92. How to enable HTTP 2.0 in a Spring Boot application?

server.http2.enabled=true

93. What is Spring Boot actuator?

Spring Boot actuator supports monitoring and application management of a Spring Boot application.

94. How to enable Spring Boot actuator?

Add this as part of your application’s Mavan dependency:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

95. What is @SpringBootApplication annotation?

@SpringBootApplication is a meta annotation combining @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan.

96. What is @SpringBootConfiguration annotation?

@SpringBootConfiguration annotation is used to support Java based configuration in a Spring Boot application.

97. What is @EnableAutoConfiguration annotation?

@EnableAutoConfiguration annotation is used to apply auto-configurations available via. different starter project dependencies in Maven or Gradle.

Check this article for more detailed questionnaire on Spring Boot.

With this, we are finished with our exhaustive list of Spring Interview Questions and Answers.

Leave a Comment