Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

26 Spring Framework Interview Questions Answers for 2 to 4 years experienced Java Developers

Spring is one of the most popular framework and most widely used to develop Java Web application using MVC framework. Due to its immense popularity its expected from Java guys to know how to use Spring framework and how it works. Most of the Java development position uses Spring, Hibernate along with core Java and JEE web technologies like Servlet, JSP, and JSF. In this article, I will be sharing some frequently asked Spring interview question for Java JEE developers. It will contain questions from different Spring modules like Spring MVC, Spring core, the concept on which Spring is built e.g. IOC and DI and Spring security.


Top 26 Spring Framework Interview Questions and Answers

Here is my list of top 20 Spring framework interview questions and answers for Java and JEE developers having 0 to 4 years of experience.


1. What is Spring framework? What is the main advantage of using Spring?
Spring is a Java based framework which provides essential features required by Java application out-of-the-box e.g. dependency injection, transaction control, JDBC and JMS helper classes. Provides support to integrate with other frameworks e.g. Hibernate, out-of-box web security features using Spring security etc.


2. What is difference between @Component, @Controller, @Repository, and @Serverice annotation in spring?
All three are Spring beans but their responsibility is different depending upon the layer they are used, for example @Controller is used in presentation layer, @Service is used in service layer, and @Repository is used in Data access layer.  If you wan to learn them in depth, you can also read my earlier article about the same topic, here

Difference between @Component, @Controller, @Repository, and @Serverice annotation in spring?




3. What is different bean scope for Spring beans?
singleton, prototype, request, and session


4. What is the difference between singleton and prototype bean scope?
singleton means just one instance shared between every client who calls the getBean() method, prototype means every time getBean() is called a new instance of the bean is get created.

spring bean scope and differences



5. Does spring beans are thread-safe?
No, spring doesn't provide any thread-safety guarantee, but prototype beans are thread-safe because they are not shared between multiple threads but singleton beans are not thread-safe.


6. What is the default scope of bean in Spring framework?
The default scope is the singleton, which means if you declare bean without specifying scope then it will be a singleton.


7. How does the Spring bean post processor work?
Here is a nice diagram which explains how exactly the Spring BeanPostProcessor work in Spring Framework

How does the Spring bean post processor work?




8. What is dependency injection?
Dependency injection is a design pattern in which object's dependency is provided to it rather than object gathering its dependency and increasing coupling between modules. Spring framework provides a DI container which can be used to manage dependency of objects.


9. What is the difference between Dependency injection (DI) and Inversion of Control (IOC)?
Both are similar thing but first is pattern of providing dependency to object while second is a concept where role of creating object or passing dependency is reversed from developer to framework like Spring or Google Guice.


10. What are the different types of dependency injection (IoC) supported by Spring?
Spring support two main types of dependency injection, constructor injection and setter injection. In former dependencies are passed via constructor and in case of later, dependencies are passed via setter methods.




11. What is the difference between Setter and Construction injection?
Differences comes from the fact that in case of setter injection, you call setter methods to pass dependencies while in case of constructor, you pass dependencies via constructor. This also means that in case of setter injection, dependencies are passed after object is created while in case of constructor injection, dependencies are passed when object is created. 

That's why constructor injection is often used to pass mandatory dependencies without which object cannot function and setter injection is passed additional or optional dependencies. 


12. When should you use setter vs constructor injection in Spring?
Use constructor injection to pass mandatory dependencies without which object cannot work while use setter injection to pass additional dependencies after object is created. 


13. What is the difference between BeanFactory and ApplicationContext?
Both are responsible for creating and managing beans in spring framework but later has more functionality and provide internationalization support.  Here are few more differences between BeanFactory and ApplicationContext in Spring framework:



14. What are different spring modules?
There are many projects or modules in spring framework like Spring Boot, Spring Data JPA, Spring Security, Spring Cloud etc. 


15. What are the lazily instantiated beans in Spring framework?
This means beans are not created and initialized until they are required by any object or code. 


16. What are inner beans in Spring framework?
A bean inside another bean is called the inner bean in spring Framework.


17. What is auto wiring means in Spring?
Normally, many Java developer defines the relationship between bean using setter and constructor injection in Spring configuration file e.g. applicationContext.xml but Spring also provides an option to figure out relationship and dependency by itself, this is known as auto-wiring in Spring.




18. What are the different types (modes) of auto-wiring supported by Spring?
Spring support XML based auto-writing or Java based auto-wiring


19. What is annotation auto-wiring? How do you enable it in Spring?
Annotation auto-wring lets you annotated property for auto-wiring. You can use @Autowried annotation to annotate a property in Spring.  Depending upon which Spring version you are suing it may not be turned on in the Spring container. So, before you can use the annotation-based wiring, you need to enable it by using context:annotation-config

20. Can you list down some limitations of auto wiring in Spring?


21. What is @Required annotation does in Spring?
@Required annotation is used to specify the mandatory dependency of a bean. Spring container throws BeanInitializationException if the @Required annotated bean property has not been populated.


22. What is DispatcherServlet in Spring MVC?
The DispatcherServlet is a core of Spring MVC, it's the front controller which handles all the HTTP requests and responses.


23. What is the difference between WebApplicationContext and ApplicationContext?
WebApplicationContext is an extension of ApplicationContext for the web application. It has more features required for web application e.g. it is capable of resolving themes, and that it knows which servlet it is associated with.


24. What are the two types of transaction management supported by Spring?
Spring support both declarative and programmatic transaction management. @Transaction annotation is used for declaration transaction management. 

transaction management in spring



25. What do @Controller and @ReuestMapping annotation do in Spring MVC?
Both @Controller and @RequestMapping are Spring MVC specific annotations, former is used to make a class controller while later is used to map the request with a method. Another difference between @Controller and @RequestMapping is that @Controller is applied at class level while @RequestMapping is applied at method level.  


26. What does the ViewResolver do in Spring MVC?
The ViewResolver is used for resolving view corresponding to request or response URL. It usually defines the prefix and suffix to resolve the view component.

27. How can you use List, Set, and Map in Spring configuration file?
This questions is for you to answer, let me know how will you do that in comments

28. How do you use a properties file in the Spring configuration file?
This is another question for you to answer, think about @PropertySource


That's all about Spring Interview questions and answers for 3 to 5 years experienced Java programmers and developers . These questions will help you in your telephonic round of interviews as well as face-to-face interviews. Even if you are not an expert, you will get some basic idea of some important features of Spring framework, which is a very sought-after job skill for Java developer positions. If you can learn to Hibernate along with Spring then it will further boost your chance to get a Java web developer job.

Other Java and Spring articles you may like
  • 15 Spring Data JPA Interview Questions with answers (questions)
  • 5 Courses to learn Spring Cloud and Microservices (courses)
  • 13 Spring Boot Actuator Interview questions (Spring actuator qustion)
  • 15 Spring Cloud Interview Questions for Java developers (answers)
  • Top 5 Courses to learn Microservices in Java? (courses)
  • 15 Microservices Interview questions (answers)
  • 5 Course to Master Spring Boot online (courses)
  • 10 Spring MVC annotations Java developers should learn (annotations)
  • Top 5 Books and Courses to learn RESTful Web Service (books)
  • 3 ways to change Tomcat port in Spring Boot (tutorial)
  • 5 Spring Boot Annotations for full-stack Java developers (tutorial)
  • 10 Tools Java Developers use in their day-to-day life (tools)
  • 10 Courses to learn Spring Security with OAuth 2 (courses)
  • 10 Advanced Spring Boot Courses for Java Programmers (courses)
  • 3 Best Practices Java Programmers can learn from Spring (best practices)
  • 5 courses to learn Spring Boot and Spring Cloud ( courses)
Thanks for reading this article so far; if you find these Spring boot Actuator interview questions and answers useful, please share them with your friends and colleagues.

P. S. - If you want to learn about Spring Boot and look for a free Spring Boot online course, I also recommend you join one of these free Spring Framework online courses on Udemy. It's one of the best free courses to learn Spring Boot for Java developers. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.