Top 10 Spring Interview Questions Answers J2EE

Spring framework interview questions are on the rise on J2EE and core Java interviews,  As Spring, is the best 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. With this popularity interview, questions from spring framework are top on any list of Core Java Interview questions. I thought to put together some spring interview questions and answers which have appeared on many Java and J2EE interviews and useful for practicing before appearing on any Java Job interview. This list of Spring interview questions and answers contains questions from Spring fundamentals e.g. Spring IOC and Dependency Injection, Spring MVC Framework, Spring Security, Spring AOP etc, because of 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 J2EE interviews. I suggest preparing those as well.

Anyway, these Spring questions are not very difficult and based on fundamentals e.g. What is default scope of Spring bean and mostly asked during 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 some answers for quick reference. 

As I have said it before both Spring and Spring MVC is fantastic Java framework and if you are not using it 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 application as well as RESTful Web Services using Spring. 

Starting with Spring 4.0, the framework has provided several useful annotations e.g. @RestController to simply the development of RESTful Web Services in Spring. You can also see REST with Spring course by Eugen Paraschiv to learn more about developing and security real world RESTful web services using Spring framework. 

Top Spring Interview Questions and Answers

spring interview questions and answers J2EENow let’s start with questions, these Spring Interview Questions are not very tricky or tough and based upon primary concepts of spring framework. If you are developing an application using 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 to any Spring and Java interview. 

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 with Spring, so don't forget to prepare some Hibernate interview questions along with Spring.

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 few of Spring security question I came across. Here are those:

How do you setup LDAP Authentication using Spring Security? 
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 configuration in Spring config file you can have this feature enable. See How to perform LDAP authentication in Java using Spring Security for detailed code explanation and sample.

Spring Security Interview Questions answers in Java

How do you control concurrent Active session using Spring Security?
Another Spring interview question which is based on Out of box feature provided by Spring framework. You can easily control How many active session a user can have with a Java application by using Spring Security. See this spring security example of how to control concurrent session in Java and Spring for exact details.

Question1: What is IOC or inversion of control? (answer)
Answer: This Spring interview question is the first step towards Spring framework and many interviewers start Spring interview from this question. As the name implies Inversion of control means now we have inverted the control of creating the object from our own using new operator to container or framework. Now it’s the responsibility of 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" class="springexample.stockMarket.CreateNewStockAccont">
        <property name="newBid"/>
In this example, CreateNewStockAccont class contain getter and setter for newBid and 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 annotation it can be done automatically by Spring, referred as auto-wiring of bean in Spring.

Question 2: Explain the Spring Bean-LifeCycle.

interview questions on Spring framework
Ans: Spring framework is based on IOC so we call it as IOC container also So Spring beans reside inside the IOC container. Spring beans are nothing but Plain old java object (POJO).
Following steps explain their life cycle inside the container.
1. The container will look the bean definition inside 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 method destroy() 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.
To learn more about spring beans and their life cycle I also suggest you take a look at these Spring Framework books.

Question 3: what is Bean Factory, have you used XMLBeanFactory?
Ans: BeanFactory is factory Pattern which 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 instance 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 very popular Spring interview question and often asks in entry level interview. ApplicationContext is the preferred way of using spring because of functionality provided by it and 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 JNDI access, EJB integration, remoting
Doesn’t support.

Question 5: What are 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

Question 6: What is the difference between singleton and prototype bean?
spring questions answers
Ans: This is another popular spring interview questions 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 spring framework are singleton beans. There is an attribute in bean tag named ‘singleton’ if specified true then 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 spring framework are by default singleton beans.

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

Question 7: What type of transaction Management Spring support?
Ans: This spring interview question is 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 applications 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 type of transaction management is supported by spring:
1. Programmatic transaction management
2. Declarative transaction management.

Question 8: 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 cuts 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 Spring MVC interview questions for more focus on Java web development using Spring framework. 

Question 9: 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 10: What are joint Point and point cut?
Ans: This is not really a spring interview questions I would say an AOP one.  Similar to Object oriented programming, AOP is another popular programming concept which 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 point cut is something that defines at what join-points an advice should be applied.

Here are few more Spring fundamental interview questions for practice

Question 11: Difference between the setter and constructor injection in Spring? (answer)
Question 12: How to implement Role Base Access Control (RBAC) using Spring Security? (answer)
Question 13: How to call the stored procedure from Java using Spring Framework? (answer)
Question 14: How to Setup JDBC Database connection pool in Spring Web application? (answer)
Question 15: Difference between Factory Pattern and Dependency Injection in Java? (answer)

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 of those question on some other interview article. I would also suggest that share some spring questions asked to you guys during the interview and then I can put together those with their answers for quick reference of everybody.

Further Reading
Expert Spring MVC and Web Flow (read here)
Spring Fundamentals By Bryan Hansen (see)
Introduction to Spring MVC 4 By Bryan Hansen (see)
Hibernate Interview Questions and answers (see here)

P.S. - If you are an experienced Java/JEE Program and want to learn Spring Security end-to-end, I recommend Learn Spring Security course by Eugen Paraschiv, The definitive guide to secure your Java application. It's useful for both junior and experienced Java Web developers.

He is also the author of REST with Spring course, one of the best online course to learn RESTful WebServices using Spring framework.


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

arpan srivastava 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???

arpan srivastava 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???

sskcar said... are so confusing and your english is funndy. Some needs pdf's some needs help ....guys do your homework and work hard ..there is no free lunch. Don't be so stupids!! are a shame to 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?

Nikita Khairnar 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..

Nikita Khairnar 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.

Nikita Khairnar 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.

Nikita Khairnar 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

Nam NVT 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

Chandan Ons 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.

Sohan Joshi 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.

Post a Comment