Thursday, September 29, 2011

Top 10 Spring Interview Questions Answers J2EE

Spring framework interview questions is in 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 de-facto framework for all new Java development. With this popularity interview questions from spring framework is top on any list of  core Java Interview questions. I thought to put together some spring interview questions and answers which has 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 to prepare those as well. Any way 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 telephonic round of Java interview. Although you can find answers of these Spring interview questions by doing Google but I have also included some answers for quick reference. As I said Spring  and Spring MVC is fantastic Java framework and if you are not using it than start using it, these questions will give you some head start as well.


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 application using Spring framework than 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.


Spring Security Interview questions Answer
Some of the reader requested to provide Spring Security interview questions and answer, So i though 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.

How do you control concurrent Active session using Spring Security?
Another Spring interview question which is based upon 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: This Spring interview question is first step towards Spring framework and many interviewer starts 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 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 framework.

Example:
  <bean id="createNewStock" class="springexample.stockMarket.CreateNewStockAccont">
        <property name="newBid"/>
  </bean>
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, refereed as auto-wiring of bean in Spring.


Question 2: Explain 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 container.
1. 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.

Question 3: what is Bean Factory, have you used XMLBeanFactory?
Ans: BeanFactory is factory Pattern which is based on IOC design principles.it is used to make a clear separation between application configuration and dependency from actual code.
XmlBeanFactory is one of the implementation of bean Factory which we have used in our project.
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"));
Or
ClassPathResource resorce = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(resorce);

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

ApplicationContext.

BeanFactory
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
Doesn’t support.
Support internationalization (I18N) messages
It’s not
Support application life-cycle events, and validation.
Doesn’t support.
Support  many enterprise services such JNDI access, EJB integration, remoting
Doesn’t support.



Question 5: What are different modules in spring?
Answer : spring have 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 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 defines 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"/>
  </bean>



Question 7: What type of transaction Management Spring support?
Ans: This spring interview questions 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.  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 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

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 is 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. Join point is an opportunity within 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

These spring interview Questions and answers are not very difficult and focused on spring fundamentals rather than focusing on 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 interview and than I can put together those with there answers for quick reference of everybody.

25 comments :

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

@Priyanshu

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

Allam Kumar said...

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

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.

reards,
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...

Sir
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 asp.net or any other html files.
Will you please give me result???

arpan srivastava said...

Hi,
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 asp.net or in completrly html form
Please Tell me how can i do it???

sskcar said...

Arpan....you 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!!...you 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.

Post a Comment