Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Saturday, May 4, 2024

Top 22 Spring Interview Questions Answers for Java Programmers [UPDATED]

Hey Java Programmers, if you are preparing for a Java developer interview then you should pay special attention to Spring framework-related questions. Spring framework interview questions are on the rise on Java web development and core Java interviews.  This is obvious given Spring Framework is the best most popular framework available for Java application development and now Spring IOC container and Spring MVC framework are used as a de-facto framework for all new Java development. Because of its popularity,  interview questions from the spring framework are top on any list of Java Interview questions and you should also prepare different Spring topics like Spring Boot, Spring Data JPA, Spring Security, Testing and Spring Cloud, etc to do well on Java developer interviews. 

After some of my readers requested, I thought to put together some spring interview questions and answers which have appeared on many Java interviews and useful for practicing before appearing on any Java Job interview. 

I first wrote this article a long back and even its content is still relevant I thought to update it, especially after finishing my list of Spring Boot Interview Questions recently.

This list of Spring interview questions and answers contains questions from Spring fundamentals like Spring IOC and Dependency Injection, Spring MVC Framework, Spring Security, Spring AOP, etc, because of the length of this post I haven't included Spring interview questions from Spring JDBC and JMS which is also a popular topic in core Java and Java EE interviews. I suggest preparing those as well.

Anyway, these Spring questions are not very difficult and based on fundamentals like what is the default scope of Spring bean? which is mostly asked during the first round or the telephonic round of Java interview. 

Although you can find answers to these Spring interview questions by doing Google I have also included answers for most of the questions for your quick reference.

As I have said before both Spring and Spring MVC, and now Spring Boot are fantastic Java frameworks and if you are not using them then it's a good time to start using them, these questions will give you some head start as well. 

Spring MVC can be used to develop both standalone Java web applications as well as RESTful Web Services using Spring.

If you are completely new to the Spring framework, I suggest you first take a look at Spring Framework 6: Beginner to Guru course, otherwise, most of these questions will not make any sense to you. It's also one of the most up-to-date courses which not only cover the basics of IOC and DI container but also advanced concepts like reactive programming with Spring 5.

22 Spring Framework Interview Questions and Answers

These articles cover Interview questions from different Spring Framework projects like Spring MVC, Core Spring, and Spring with REST and Spring MVC particularly. I have also listed resources and linked some articles to learn more and get some more practice questions if you are interested in further learning. 

Now let’s start with questions, these Spring Interview Questions are not very tricky or tough and based upon primary concepts of the spring framework. 

If you are developing an application using the Spring framework then you may be, already familiar with many of these Java and Spring interview questions and answers. Nevertheless, it’s a good recap before appearing in any Spring and Java interview.

1. Spring Core Interview Questions

Question 1: What is IOC or inversion of control? (answer)
Answer: This Spring interview question is the first step towards the Spring framework and many interviewers start Spring interviews from this question. 

As the name implies Inversion of the control means now we have inverted the control of creating the object from our own using a new operator to container or framework.

Now it’s the responsibility of the container to create an object as required. We maintain one XML file where we configure our components, services, all the classes, and their property. We just need to mention which service is needed by which component and container will create the object for us.

This concept is known as dependency injection because all object dependency (resources) is injected into it by the framework.

 <bean id="createNewStock" 
        <property name="newBid"/>

In this example, the CreateNewStockAccont class contains the getter and setter for newBid, and the container will instantiate newBid and set the value automatically when it is used. This whole process is also called wiring in Spring and by using annotations it can be done automatically by Spring, referred to as auto-wiring of beans in Spring.

Spring Interview Questions Answers for Java Programmers

Question 2: Explain the Spring Bean-LifeCycle.

Ans: Spring framework is based on IOC so we call it an IOC container also So Spring beans reside inside the IOC container. Spring beans are nothing but Plain old java objects (POJO).
The following steps explain their life cycle inside the container.

1. The container will look at the bean definition inside the configuration file (e.g. bean.xml).

2 using reflection container will create the object and if any property is defined inside the bean definition then it will also be set.

3. If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID.

4. If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.

5. If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization() methods will be called before the properties for the Bean are set.

6. If an init() method is specified for the bean, it will be called.

7. If the Bean class implements the DisposableBean interface, then the destroy() method will be called when the Application no longer needs the bean reference.

8. If the Bean definition in the Configuration file contains a 'destroy-method' attribute, then the corresponding method definition in the Bean class will be called.

These were just some of the Spring Fundamentals I can cover here if you are interested to learn more I suggest you take a look at Spring Master Class - Beginner to Expert, an end-to-end course to learn Spring.

Best Spring Framework Interview Questions Answers

Question 3: What is Bean Factory, have you used XMLBeanFactory?
Ans: BeanFactory is a factory Pattern that is based on IOC design is used to make a clear separation between application configuration and dependency from actual code. The XmlBeanFactory is one of the implementations of Bean Factory which we have used in our project.

The org.springframework.beans.factory.xml.XmlBeanFactory is used to create bean instances defined in our XML file.
BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml"));
ClassPathResource resorce = new ClassPathResource("beans.xml"); 
XmlBeanFactory factory = new XmlBeanFactory(resorce);

Question 4: What are the difference between BeanFactory and ApplicationContext in Spring? (answer)
Answer: This one is a very popular Spring interview question and often asks in an entry-level interview. ApplicationContext is the preferred way of using spring because of the functionality provided by it and the interviewer wanted to check whether you are familiar with it or not.


Here we can have more than one config files possible
In this only one config file or .xml file
Application contexts can publish events to beans that are registered as listeners
Don't support.
Support internationalization (I18N) messages
It’s not
Support application life-cycle events, and validation.
Doesn’t support.
Supports many enterprise services such as JNDI access, EJB integration, remoting
Doesn’t support.

Question 5: What is AOP?
Answer: The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules. AOP is a programming technique that allows a developer to modularize crosscutting concerns, that cut across the typical divisions of responsibility, such as logging and transaction management. 

Spring AOP, aspects are implemented using regular classes or regular classes annotated with the @Aspect annotation. You can also check out these 30+ Spring MVC interview questions for more focus on Java web development using the Spring MVC framework. 

Question 6: Explain Advice?
Answer: It’s an implementation of aspect; advice is inserted into an application at join points. Different types of advice include “around,” “before” and “after” advice

Question 7: What are the joint Point and point cut?
Ans: This is not really a spring interview question I would say an AOP one.  Similar to Object-oriented programming, AOP is another popular programming concept that complements OOPS. A join point is an opportunity within the code for which we can apply an aspect. In Spring AOP, a join point always represents a method execution.

Pointcut: a predicate that matches join points. A pointcut is something that defines what join-points advice should be applied.

If you need more Spring AOP questions then you can also check out my article about 17 Spring AOP Interview Questions with Answers for Java developers. 

Question 8: Difference between the setter and constructor injection in Spring? (answer)
Setter injection is more flexible than constructor injection because you must remember the type and order of the constructor parameters. Also, constructor injection is generally used to inject the mandatory dependency, while setter can be used to inject the optional dependency.

Question 9: Difference between Factory Pattern and Dependency Injection in Java? (answer)
Even though both allow you to reduce coupling in code, dependency injection is much more flexible and easier to test than Factory pattern.

Questions 10. Difference between @Autowired and @ Inject annotation in Spring? (answer)

Question 11: What are the different modules in spring?
Answer: spring has seven core modules
1.      The Core container module
2.      Application context module
3.      AOP module (Aspect Oriented Programming)
4.      JDBC abstraction and DAO module
5.      O/R mapping integration module (Object/Relational)
6.      Web module
7.      MVC framework module

2. Spring MVC Interview Questions Answers

So far we have seen the Spring core interview questions and answers and not let's see some interview questions from Spring MVC, one of the most important parts of Spring farmwork which allows you to develop web applications in Java using Model View Controller design pattern. 

Questions 12: What is the difference between @Controller and @RestController in Spring MVC? (answer)
Even though both are used to indicate that a Spring bean is a Controller in Spring MVC setup, @RestController is better when you are developing RESTful web services using the Spring MVC framework. It's a combination of @Controller + @ResponseBody annotation which allows the controller to directly write the response and bypassing the view resolution process, which is not required for RESTful web service.

It also instructs DispatcherServlet to use different HttpMessageConverters to represent the response in the format client is expecting e.g. HttpMessageJackson2Convert to represent response in JSON format and JAXB based message converts to generate XML response.

You can further see the REST with Spring course by Baeldung to learn more about developing RESTful Web Services using Spring 4 and Spring 5.

Top 22 Spring Interview Questions Answers for Java Programmers

Question 13: What is the difference between a singleton and a prototype bean?
Ans: This is another popular spring interview question and an important concept to understand. Basically, a bean has scopes which define their existence on the application.

Singleton: means single bean definition to a single object instance per Spring IOC container.

Prototype: means a single bean definition to any number of object instances.
Whatever beans we defined in the spring framework are singleton beans.

There is an attribute in the bean tag named ‘singleton’ if specified true then the bean becomes singleton and if set to false then the bean becomes a prototype bean.  By default, it is set to true. So, all the beans in the spring framework are by default singleton beans.

  <bean id="createNewStock" class="springexample.stockMarket.CreateNewStockAccont"
        <property name="newBid"/> 

Question 14: What is the role of DispatcherServlet in Spring MVC? (answer)
The DispatcherServlet is very important from the Spring MVC perspective, it acts as a FrontController i.e. all requests pass through it. It is responsible for routing the request to the controller and view the resolution before sending the response to the client.

When Controller returns a Model or View object, it consults all the view resolvers registered to find the correct type of ViewResolver which can render the response for clients.

In the case of RESTful Web Services, the DispatcherServlet is also responsible for using HttpMessageConverter to represent the response in the JSON, XML, or TEXT format, depending on the content negotiation between Client and Server like if the client sends a request with HTTP accept header as "application/json" then DispatcherServlet will ask the HttpMessageJackson2Converter to convert the response into JSON format.

You can further see the free Introduction to Spring MVC course from Pluralsight to learn more about Spring MVC and DispatcherServlet.

spring interview questions and answers for beginners

Question 15: How to call the stored procedure from Java using Spring Framework? (answer)

Question 16: How to Setup JDBC Database connection pool in Spring Web application? (answer)

Questions 17: Difference between @ReqeustParam and @PathVariable in Spring MVC? (answer)
The @ReqestParam annotation can be used to rad query parameters while @PathVariable annotation can be used to read data from URL.

Questions 18: Difference between @Component, @Service, @Controller, and @Repositoring annotation in Spring MVC? (answer)
All of them are stereo type annotations but @Component is used to denote any Spring bean while @Controller is used in Spring MVC to indicate that a class is acting as a controller as per MVC pattern. Similarly, @Service indicate that the class handles business logic and @Reponsitory is for data access layer.

Question 19: What type of transaction Management Spring support?
Ans: This spring interview question is a little difficult as compared to previous questions just because transaction management is a complex concept and not every developer familiar with it. Transaction management is critical in any application that will interact with the database.

The application has to ensure that the data is consistent and the integrity of the data is maintained.  Following two types of transaction management is supported by spring:

1. Programmatic transaction management
2. Declarative transaction management.

If you need more questions, you can also check out the Java Programming Interview exposed book from Wrox publication, apart from various Java topics it also contains some really good Spring framework, Hibernate, and Spring MVC questions with detailed explanation.

Spring Framework interview questions for Java programmers

Also, Hibernate is mostly used in Spring, so don't forget to prepare some Hibernate interview questions along with Spring.

3. Spring Security Interview Questions Answer

Some of the readers requested to provide Spring Security interview questions and answer, So I thought to update this article with a few of the Spring security questions I came across.

Here are those:

20. How do you control the concurrent Active session using Spring Security? (answer)
Another Spring interview question is based on Out of box feature provided by the Spring framework. You can easily control How many active sessions a user can have with a Java application by using Spring Security.

Apart from that Spring Security also provides the "remember me" feature which you can use to provide easier access for your users by remembering their login details on their personal computer. You can further see Learn Spring Security course by Eugen Paraschiv to learn more about advanced details of Spring Security. 

21. How do you set up LDAP Authentication using Spring Security? (answer)
This is a very popular Spring Security interview question as Spring provides out-of-the-box support to connect Windows Active Directory for LDAP authentication and with few configurations in the Spring config file you can have this feature enabled.

22.  How to implement Role-Based Access Control (RBAC) using Spring Security? (answer)
Spring Security provides a couple of ways to implement Role-based access control like by using GrantedAuthority. See the article to learn more about it.

That's all about frequently asked Spring Interview Questions with Answers. These Spring interview Questions and answers are not very difficult and focused on spring fundamentals rather than focusing on an advanced feature of session management, spring security, authentication, etc. we will cover those questions in some other interview article. 

I would also suggest that share spring framework questions asked to you guys during the interview and then I can put together those with their answers for quick reference of everybody.
Thanks for reading this article, if you like this article then please share it with your friends and colleagues. If you have been asked any question from this list then please let us know in comments and if you have any other question which you want to share, go for it. 

P. S. - If you are preparing for Java Interviews and need more Spring Framework Interview questions for Practice then you can also check out this Spring Framework Interview Guide - 200+ Questions & Answers, which contains 200+ real-world questions and their explanations.


Anonymous said...

Spring is largely a redundancy. Java EE 6 provides what you are likely to need from Spring a standards compliant framework.

If you are still doing J2EE development I can understand why you think Spring is the better framework, however.

Javin @ java OutOfMemoryError said...

@Anonymous, Thanks for your comments, In My opinion Spring is better than J2EE , though this is a premature comment because I have not used J2EE quite except Jsp , Servlet and EJB a lot but with that limited experience I found Spring more better.

Javin @ Java Generic tutorial said...

Thanks Thomas you like these spring questions, and thanks for explaining that point some how I completely missed that.

Raja said...

Can you add some more Spring framework interview questions , especially from spring mvc part. I am going for spring interview and expecting spring mvc interview questions based on job description. Also Can you please upload pdf version of spring interview questions so that I can take a print out.

Raja said...

Also it would have been little better if you could split up these spring interview question on different category e.g. Spring IOC interview questions, Spring AOP interview questions, Spring MVC interview questions, Spring security interview questions , Spring dependency injection interview questions etc.

Sarabdeep said...

I think Spring is awesome framework and very modular. One of best frameworks around in Open Source Projects. I am just overwhelmed with features and abilities provided by these framework. I don't this it needs anyone approval. Most of bigger firms use it in bigger projects for scalability. Love Live Spring Framework.....

Anonymous said...

these spring interview questions are rather easy, you should include touch spring questions doesn't matter whether it appeared on interview or not.

Gaurav said...

can you also put some spring mvc questions, like questions on controller, dispatch-servlet.xml and view mappings on spring framework. spring-mvc is more asked than any other module of spring framework so please share some spring mvc interview questions.

Anonymous said...

@Raja, did you get the job you were interviewing for? Doubt it since you can't cut and paste the Q & A in this article and print them on your own. Please make me a pdf...whaaaa

Priyanshu Joshi said...

Here are few more challenging and tough Spring interview question:

1) What is Spring IOC Container ?
2) Does Singleton from Spring Container is thread safe?
3) Why Spring MVC is better than Struts?
4) What is Spring Integration?
5) How to call remote method by RMI using Spring
6) What scheduling feature Spring framework provides?
7) How do you make a Singleton bean to lazy load in ApplicationContext which loads all Singleton beans eagerly during startup?
8) Does Spring Security part of Spring framework?
9) How to configure Spring using Annotation
10) Which version of Spring have you used recently and what is difference you observed from previous spring version.

Saumya said...


Answer of "What is Spring IOC Container"
Spring IOC container is an implementation of Dependency Injection design pattern provided by Spring framework. Instead of creating objects in application code using new() keyword, we use ApplicationContext.getBean("nameOfBean") to get reference of Objects. Spring framework creates objects, manages object and set the dependency automatically so that you can get Object in proper state.

Answer 2 - "Does Spring Singleton are thread-safe"
I am not sure on this , I guess this may depends upon whether object is immutable or not.

Sneha said...

Hi, I am an experienced Java programmer (4 years experience in Java and J2EE technology) but I am new to Spring framework. I am going to appear for client interview this week and looking for some Spring questions for 2 to 4 years experienced programmer. Would preparing above Spring question would be sufficient? if possible can you please share some tough, difficult or triky Spring interview question as most of them are quite command and frequently asked. They are also looking for Some spring MVC , Spring JDBC and Spring Security experience. Would be great if you could share few Spring MVC, JDBC and Spring Security interview question and answers as well.
Thanks you

Jeetu said...

Can you please let me know How can we download this Spring interview question answers as pdf ?

Vikash said...

Please share some questions answers for Spring hibernate integration and Spring AOP support. Also if you could include questions like What is new in Spring 3.0, Which newe features are introduced in Spring 3.0 etc, That would be fantastic.

Anonymous said...

Few of Spring JDBC interview questions, which I am aware of:
What is benefit of using JdbcTemplate ? Why should you use JdbcTemplate in Spring ?
How do you handle SQLException in Spring?
How to you setup JDBC connection pool in Spring etc.
interview questions from JDBC in Spring is not that many, but you should be familiar with JdbcTemplate.

Anonymous said...

Spring is light weight component and open source.Dependency injection based so no need to configure the class file in *.xml

sunil choudhury said...

I am new to Spring. IoC says that it overcome the creation of object. But at the same time we are using getBean(String id) instead of new operator when ever we need the bean instance.

can it be possible to get the bean instance with out using getBean() not even for a single time.

sunil choudhury

Javin @ Singleton vs Static class Java said...

Hi Sunil, IOC does lot of stuff for you. It creates dependency and construct objects. If you don't need explicit instance of bean, you can avoid that getBean() call, but you almost always need that. It's better to wrap casting and call to getBean in a separate class itself and use Generics , instead of casting everywhere in code.

Anonymous said...

question about prototype was asked to me in ML interview. The tougher question is why we need a prototype scope ?the asnwer is if you want one instance of the bean per thread or session i.e. if you need a stateful bean

Anonymous said...

What is the difference between Spring MVC and struts2 and which one is better to use

Unknown said...

I have .war file in java eclipse,but how can i upload it on server please tell me
because when i m asking to the domain giver sites they recommend me
to convert it into either or any other html files.
Will you please give me result???

Unknown said...

Dear sir
I have a query,I have coded a web site in jee,html,js
in eclipse and i got a .war file in it and i want to upload
it on server as the same,I have asked to many domain giver
how to upload it in .war file but they recommended me to use it either
in or in completrly html form
Please Tell me how can i do it???

Anonymous said...

@sskcar...Here Peoples ask for help, if you can't provide any help to them then please , you don't have right to say whatever u said in your recent comments.

Guruprasad said...

I recently attended a Spring interview and got a follow up question on Question 6.

How would you inject a prototype bean into a singleton bean from a method?

I answered that I would create the prototype bean using the application context giving the bean id inside the method of the singleton class.

But apparently there is another way of doing this:
1. Use element when creating the Singleton class
2. using the element when creating the Prototype class.

Anonymous said...

Hello dude, these Spring framework questions are good for freshers and beginners, but not good enough for experienced deveopers. Can you add some more advanced questions for senior programmers, who knows Spring basic and has over 2 to 4 yeras experience in developing Spring MVC framework?

Unknown said...

I am nikita. Currently working in bpo company. Having 1 year experience.
I want my career in java.have completed 1 project in java.
Can I switch into java bcz I am from computer background.
In java wants 3-4 year experience. And now I can't apply fresher also.
What should I do?
Anyone plz suggest me..

Unknown said...

I have one question
I am currently working in bpo company.
But I want career in java.
If I complete Project in spring .then is it possible to get hire in IT company? Without having any experience in java?

I am from computer background

javin paul said...

Hello Nikita, If you have any engineering degree or courses on computer science e.g. M.Sc or MCA, it's quite easy to get Job on Java Software development. I also suggest to do Oracle Java Certification, a good score there will boost your chances of Java job for sure. Coming to Spring, it's not mandatory for Java, but having this experience is always good. Good luck.

Unknown said...

I have completed bcs .no any certification in java .I thought with spring I can enter into java and Will no wastage of experience.

javin paul said...

I presume, BCS is a bachelors degree in computer science, so that's good. If you don't have any experience than I suggest to do certification and at same time try to work as intern or trainee to gain some experience. Having said that, no harm in trying and giving Job interviews. There are times when company needs developer in large numbers and they are less restrictive in filtering. Having worked in Spring will also help you.

Unknown said...

Thanks Javin for better suggestions.I
Will prefer this one. One more question which certification would be best ocjp or scjp?

javin paul said...

Your Welcome Nikita, Go for OCJP, it's new name of SCJP after Oracle acquired Sun in 2010

Unknown said...

Hi Guruprasad,

I still dont understand your point. Could you pls give us an example?

Anonymous said...

All beans created in Spring are by default Singleton but not thread safe.

Anonymous said...

i want my career in java.i have no experience .so pls suggest me .how get job in java developer.

Anonymous said...

Thanks for such an useful topic.
In one interview I face the question that is if we declare bean with same class but different ID's then will spring container create more object or it creates single object as we are creating these beans with singletone scope.
Is there any difference in singletone design pattern and singletone bean scope?

Anonymous said...

Very useful questions on Spring, for more Java interview questions from last 5 years, You can also see this list of lastest Java questions

Unknown said...

How to explain spring mvc along with your current project online and what are the module should i explain plz help me out

Unknown said...

how to deal with cyclic dependency in spring config xml is also a popular question asked to me & one of my friend too..

Bean A --->BeanB
BeanB ---> BeanA

Anonymous said...

Can you include some questions/answers on spring batch topic

Unknown said...

Without learning j2EE we can understand spring and hibernet concept.

javin paul said...

@chandan Ons, you are correct now there is no need to learn Java EE to use Spring or Hibernate, you can even use them in core Java project which requires database access. Spring is now mainly use for DI and Hibernate is for ORM.

Unknown said...

Can you please explain some basic benefits of Dependency Injection ?

Jagadeesh said...

for your 3rd question answer..
do we have singleton=”false/true” attribute in bean definition ? I think it is scope attribute.

harsh said...

what is dispatcher servlet

javin paul said...

It's Servlet implementation in Spring MVC which works as front controller. All the request to your web application goes through this class in spring mvc application.

Anonymous said...

Can any one please answer these spring questions for me

Which annotation is used to enable Kafka?

How to handle cyclic dependency between beans? Let’s say for ex: Bean A is dependent on Bean B and Bean B is dependent on Bean A.How does the spring container handle eager & lazy loading?

How to set the properties across different environments like Dev, QA, PROD, etc.
Describe the AOP concept and which all annotations are used. How to define the pointcuts?

How to handle a transaction and the isolation levels of the transaction?

Post a Comment