Friday, January 20, 2023

Top 30 Spring Core, Spring MVC and Spring Security Interview Questions Answers

Spring Interview Question is one of the first things Java programmers should prepare before appearing on any Java or Spring interview. With the immense popularity of Spring frameworks like Spring MVC and Spring Boot in the Java world, the focus on Spring skills on interviews increases daily. For those who don't know, the Spring framework is one of the most famous structures for Java applications, which aims to simplify Java development. It revolutionized the Java development when it initially came with concepts of  Dependency Injection and Inversion of Control, which made writing a testable and maintainable Java application

But the Spring framework just didn't stop there; it also provides valuable API to Java programmers. The best thing is that Spring has been evolving since its inception and addressing Java developers' concerns in different areas.

The spring framework is divided into many different modules like Spring MVC, Spring Integration, Spring Batch, Spring LDAP, Spring Security, Spring Boot, Spring Cloud, and several other modules that focus on a particular area.

For example, Spring Boot tries to make it even easier to use the Spring framework by reducing the amount of configuration you need to put in for Spring itself. Spring Cloud is evolving to become the best framework for developing cloud-based Java frameworks and Microservices, which is a need for the future. Spring Security has established itself as the go-to framework for security Java web applications.

Based on your work experience, the interviewer can ask questions from core Spring or these modules. That's why, while preparing the Spring interview question focus on Spring core, Spring Security, and Spring API, these are the main areas from where the interviewer asks questions.

If you have working experience in Spring-based Java projects, you can quickly answer most of these Spring questions from several Java interviews. Still, suppose you don't have any prior experience and just learning Spring. In that case, I suggest you go through the Spring Framework 5: Beginner to Guru course on Udemy to learn Spring fundamentals before attempting these questions.





30+ Spring Framework Interview Questions and Answers

Now that you know the importance of Spring Framework for Java developers and are ready to take on Spring questions in your following interview, let's see some frequently asked questions on the Spring framework on Java interviews:

1. Spring Core Interview Questions

Let's first start with the basics; in this section, you will see some Spring core interview questions focused upon the Spring framework, how it works, what benefits it provides, and dependency injection and inversion of control concepts.

1) What is the Spring framework? Why Java programmers should use the Spring framework? (answer)
Ubiquitous Spring interview question, Spring is a framework that helps Java programmers in development. Spring provides Dependency Injection and IOC container, Spring MVC flow, and several helpful APIs for Java programmers.


2) What is the default scope of bean in the Spring framework? (answer)
The default scope of a Spring bean is the Singleton scope, and in the web application default scope of a spring bean is the request scope. Singleton bean means the same instance of a bean is shared with all other beans, while request scope means a bean is alive only for a request.


3) Are Spring singleton beans thread-safe?
No, Spring singleton beans are not thread-safe. Singleton doesn't mean bean would be thread-safe.


4) What is the Dependency Injection design pattern? (answer)
Dependency Injection is one of the design patterns, which allows injecting dependency on Objects instead of object resolving the dependency.


5) What is the Inversion of Control concept, how does Spring support IOC? (answer)
The simple meaning of inversion of the control means that now the framework, Spring is responsible for creating objects, wiring dependencies, and managing their life-cycle instead of a developer, which was the case before. That's where control is inverted from developer to framework.

6) What is the difference between ApplicationContext and BeanFactory in the Spring framework? (answer)


7) Can we use more than one configuration file for our Spring project?
Yes, you can use as many as you want; all you need is to import them in the main Spring configuration file, which you will load from your program.


8) What types of dependency injection are supported by Spring Framework? When do you use Setter and Constructor Injection, the pros and cons? (answer)
There are 2 types of dependency injection supported by Spring, constructor-based injection and setter-based injection.

Both types have their own advantages and disadvantages; you should use Constructor injection when an object's dependencies are not optional, and they must be initialized with their dependencies.

Also, use constructor injection if the order of initialization or dependency matters because, in Setter based injection, you cannot impose any charge. Use setter injection when dependencies are optional. You can further see Spring Framework for Beginners with Spring Boot course by Navin Reddy on Udemy to learn more about Spring Core and IOC.

Top 30 Spring Core, Spring MVC and Spring Security Interview Questions



2. Spring MVC Interview Questions

Now that we have seen some questions on Spring core and basics, it's time for Spring MVC interview questions which is probably the most important thing because of the popularity of Spring as an MVC framework and standard for developing Java web applications.


9) What is Spring MVC? Can you explain How one request is processed? (answer)
Spring MVC is a framework to develop Java web applications. It implements MVC or Model View Controller architecture, built on the separation of concerns, and makes Java web applications easy. To use this in your project, you need to learn Spring and include its JAR file.

Spring MVC framework also supplies an inbuilt view resolver for selecting views. See Spring Master Class - Beginner to Expert to learn more about how Spring MVC internally works.

8) What is view Resolver pattern? how it works in Spring MVC



10) What is the View Resolver pattern? how it works in Spring MVC
View Resolver pattern is a J2EE pattern that allows a web application to dynamically choose its view technology, like HTML, JSP, Tapestry, JSF, XSLT, or any other view technology.

In this pattern, the View resolver holds a mapping of different views, the controller returns the name of the view, which is then passed to View Resolver for selecting an appropriate view. 


11) Explain Spring MVC flow with a simple example like starting from Container receives a request and forward to your Java application?
I have explained to them from start to end in my article how Spring MVC works internally; you can check that for a detailed answer.


12) What is the difference between Spring MVC and Spring core?
The Spring MVC is part of the Spring framework, which helps you develop Java web applications using model web controller patterns. At the same time, Spring Core provides the Dependency injection and Inversion of Control. The Spring Container is part of Spring core.

Both functionalities come in different JAR files. If you are developing just a core Java application using Spring, you just need Spring Core, but if you are creating a Web application, then you need spring-mvc.jar as well. See Introduction to Spring MVC to learn more about Spring framework architecture and components.





13) If a user checked in CheckBox and got a validation error in other fields and unchecked the CheckBox, what would be the selection status in the command object in Spring MVC? How do you fix this issue?
Since during HTTP post, if the checkbox is unchecked, then HTTP does include a request parameter for checkbox, which means updated selection won't be picked up. You can use the hidden form field, starting with _, to fix this in Spring MVC. Quite a tricky question to answer if you are not aware of HTTP POST behavior and Spring MVC.


14) What are the different implementations of the View interface you have used in Spring MVC?
U.I. based View like JSP, JSTLView,


15) How to escape HTML special characters using Spring MVC? (answer)
There are some methods in a Spring tag library; I can't remember now.


16) Can you use the Spring MVC framework along with Struts? I have an existing Java MVC application, which is based in Struts. Can I migrate that to use Spring MVC? How?


17) What is the advantage of the Spring MVC framework over Struts 1.0 or Struts 2.0 ? is it worth converting an existing Struts application to Spring MVC?


18) How does Spring resolve view returned by ModelAndView class?  (answer)


19) Difference between @SpringBootApplication and @ContextConfiguration annotation in Spring Boot? (answer)


20) What is the use of DispatcherServlet in Spring MVC? (answer)
It acts as a front controller and intercept all incoming request after that it send those request to respective controller as per their URL pattern.


21) What is the role of InternalResourceViewResolver in Spring MVC (answer)


22) What is the difference between @Autowired and @Inject annotation in Spring? (answer)


23) Difference between @RequestParam and @PathVariable in Spring MVC? (answer)


24) Difference between @Component, @Service, @Controller, and @Repository annotations in Spring MVC? (answer)
While all of these annotations are stereotype annotation, @Component is a general purpose Spring annotation to indicate that this Java class is managed by Spring or it represent a Spring bean and can be used anywhere. 

On the other @Server is supposed to be use with a Service class which  has business logic, while @Contorller should be used with any class which is handling HTTP request and @Reposistory should be used with any class which is used in Data access layer. 

As I said, they are their for best practice, even if you indicate a Repository class with @Component, compile will be fine and Spring framework will also not complain, but tomorrow, if you create a tool which can enhance all classes annotated with @Reposistory to automatically log SQL statement then this class will be left out. 

25) How to consume RESTful Web Service using Spring MVC? (answer)


26) How do you create a controller in Spring? @Controller vs. @RestController? (answer)
A controller is nothing but a class, also known as a bean in Spring terminology. If you are using annotation, then you can create a controller by using @Controller annotation.

For RESTful web services, you can also create REST controllers by using the @RestController annotation, and in that case, you don't need to use the @ResponseBody annotation explicitly to tell Spring how it needs to respond.

If you are interested in learning more about how to develop RESTful Web Service using the Spring framework, I suggest taking a look at the REST with Spring course by Eugen Paraschiv of Baeldung. It's one of the comprehensive courses covering basics and advanced concepts like versioning, testing, and deploying REST API in production.

30 Spring Framework Interview Questions for Java Programmers


3. Spring Security Interview Questions

Now that we have seen both Spring core and Spring MVC interview questions let's explore some problems from the Spring Security framework because Security is paramount. You will always find using this excellent tiny framework to secret your Java web applications.


27) What is Spring Security?
Spring security is a project under the spring framework umbrella, which provides support for security requirements of enterprise Java projects.

Spring Security, formerly known as aegis security, provides out-of-the-box support for creating a login screen, Remember me cookie support, securing URL, authentication provider to authenticate the user from the database, LDAP and in memory, Concurrent Active Session management support, and much more.

To use Spring security in a Spring MVC-based project, you need to include spring-security.jar and configure it in the application-Context-security.xml file; you can name it whatever you want. Ensure to supply this to ContextLoaderListener, which is responsible for creating Spring context and initializing the dispatcher servlet. You can further see Learn Spring Security Certification Class learn more about authentication, authorization, and session control using Spring Security.


Top 23 Spring MVC Framework Interview Questions Answers - Java JEE


28) How do you control concurrent Sessions on Java web applications using Spring Security? (answer)
You can use Spring Security to control the number of active sessions in the Java web applications. Spring security framework provides this feature out of the box, and when enabled, a user can only have one active session at a time.


29) How do you call a stored procedure by using the Spring framework? (answer)


30) What do the JdbcTemplate and JmsTemplate classes offer in Spring? (answer)
They offer a standard way to use JDBC and JMS API without writing the boiler code required to work with those API.


4. Spring Boot Interview Questions

And one last question on Spring Boot, the next generation Spring framework which makes it easy for Java developers to use the Spring framework itself:

31) Difference between @SpringBootApplication and @EnableAutoConfiguration annotations in Spring Boot? (answer)
hint - one is the old way, and the other is new. Actually, @springBootApplicaiton is a combination of three annotations, including @EnableAutoConfiguration. 


32) What is the difference between @ContextConfiguration and @SpringApplicationConfiguration in Spring Boot Testing? (answer)
hint - @ContextConfiguration doesn't take full advantage of Spring boot features while loading Spring application context for testing.


These were some of the Core Spring framework and MVC Interview questions from my collection; I have given short answers for most of these Spring interview questions. I suggest researching more or reading along those Spring questions to prepare for follow-up Spring interview questions.

Some Spring MVC questions are tricky, like Struts and Spring integration, and can be only answered by an experienced Java program with 2 to 4-year of experience in the Spring MVC framework. And if you need more questions, you can also check out the Spring Framework Interview Guide - 200+ Questions & Answers course on Udemy, where Ranga has shared frequently asked Spring interview questions.



Other Interview Questions and Articles for Java programmer:
P.S. - If you want to learn Spring 5 and Spring Boot 2 from scratch in a guided, code-focused way, I also suggest you check out Eugen Paraschiv's Learn Spring: The Master Class. He has recently launched this course after 2.5 years, which is full of exercises and examples to further cement the real world concepts you will learn from the course.

12 comments:

  1. My list of Spring MVC interview question :

    Explain Spring MVC flow with a simple example e.g. starting from Container receives request and forward to your Java application ?

    What is difference in Spring MVC and Spring core?

    Can you use Spring MVC framework along with Struts ? I have an existing Java MVC application which is based in Struts, Can I migrate that to use Spring MVC ? How ?

    What is advantage of Spring MVC framework over Struts 1.0 or Struts 2.0 ? is it worth to convert an existing Struts application to Spring MVC ?

    How does Spring resolves view returned by ModelAndView class ?

    Some Spring MVC questions are tricky e.g. Struts and Spring integration and can be only answered by experienced Java program with 2 to 4 year experience in Spring MVC framework.

    ReplyDelete
    Replies
    1. Good questions Radhe. Inspired by you, I would also like to share few more Spring MVC question which I have seen on Spring Interviews :

      1)IF User checked in CheckBox and got validation error on otherfields and than he unchecked the CheckBox, what would be selection status in command object in Spring MVC ? How do you fix this issue ?
      Since during HTTP post, if checkbox is unchecked than HTTP does include a request parameter for checkbox, which means updated selection won't be picked up. you can use hidden form field, starting with _ to fix this in Spring MVC. quite tricky question to answer if you are not aware of HTTP POST behavior and Spring MVC.

      2) What are different implementation of View interface you have used in Spring MVC ?
      ULBased View e.g. JSP , JSTLView,

      3) How to escape HTML special characters using Spring MVC ?
      there are some methods in Spring tag library, can't remember now.

      Delete
  2. Nice answer bhaumik, Can't remember now!

    ReplyDelete
  3. Please explain what is difference between spring mvc and struts 2.0?

    ReplyDelete
  4. Does anyone know the answers to the other questions?

    ReplyDelete
  5. both are used for web layer but spring MVC provide extra features

    ReplyDelete
  6. Please explain Spring MVC Controllers what are the different Cotrollers available and how to use it.and how those better then struts Action classes.

    ReplyDelete
  7. These qustions are quite good to test candidates with 1 to 3 year of experience. You can ask these questions on phone interviews as well, I have added in my own list. I particularly like the diffrence between singlton and request scope difference and whether spring beans are thread-sfae or not.

    ReplyDelete
  8. Hi Paul, amazing questions and answers.
    Regarding questions 22 there is many more classes in Spring that implement that interface. I popular example would be the TilesView class. Be careful if you use the latter because there are two: org.springframework.web.servlet.view.tiles3.TilesView and org.springframework.web.servlet.view.tiles2.TilesView and the behaviour is slightly changes.
    For this kind of questions and more you can visit our SpringCore web simulator at https://www.springmockexams.com/

    ReplyDelete
  9. Even though Spring is not part of core java I increasingly see questions from Spring in core Java interviews. thanks for sharing these, hope you could share some more questions related to performance and security aspect of Spring framework.

    ReplyDelete
  10. Hi,
    very helpful, thanks.
    I recently had a Java interview, I've been asked among the rest, about @Qualifier and @Autowired annotation.

    ReplyDelete

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